public partial class Window2 : Window |
{ |
|
private ListBox listBox; |
private UserControl userControl; |
|
public Window2(UserControl2 userControl) |
{ |
this.userControl = userControl; |
|
this.Title = "Ring elements"; |
this.Height = 350; |
this.Width = 300; |
this.Background = Brushes.WhiteSmoke; |
|
//Viewbox viewbox = new Viewbox(); |
//viewbox.Child = userControl; |
|
listBox = new ListBox(); |
StackPanel stackPanel = new StackPanel(); |
|
//this.Content = viewbox; |
this.Content = stackPanel; |
stackPanel.Children.Add(userControl); |
stackPanel.Children.Add(listBox); |
|
} |
|
protected override void OnPreviewKeyDown(KeyEventArgs e) |
{ |
MessageBox.Show("Window2"); |
|
// Raise the event |
this.RaiseEvent(new RoutedEventArgs(UserControl2.KeyboardRoutedEvent, this)); |
|
//this.Title= "Source = " + e.Source.GetType. |
|
} |
|
} |
|
public class UserControl2 : UserControl |
{ |
public UserControl2() |
{ |
//..... |
this.AddHandler(UserControl2.KeyboardRoutedEvent, new RoutedEventHandler(RoutedEvent_Raised)); |
} |
|
static UserControl2() |
{ |
UserControl2.KeyboardRoutedEvent = EventManager.RegisterRoutedEvent("RoutedKeyDown", |
RoutingStrategy.Tunnel, |
typeof(RoutedEventHandler), |
typeof(UserControl2)); |
} |
|
public static readonly RoutedEvent KeyboardRoutedEvent; |
|
public event RoutedEventHandler RoutedKeyDown |
{ |
add { AddHandler(UserControl2.KeyboardRoutedEvent, value); } |
remove { RemoveHandler(UserControl2.KeyboardRoutedEvent, value); } |
} |
|
private void RoutedEvent_Raised(object sender, RoutedEventArgs e) |
{ |
MessageBox.Show("Superb!"); |
} |
} |