Well I don't know what you mean by that exactly. I've been trying various samples that seem related.. this is what I have now, and the two button events fire. Not sure now to get the calling element, also, they dont fire if I subscribe to the events in MainWindow XAML, only if I do it in code behind. Sorry format looks strange. I have no idea how the formatting controls on this blog work whatsoever. Nothing is obvious and nothing works by default.
UserControl
<StackPanel Orientation="Vertical">
<Button x:Name="button1" Content="Button1" Width="75" Click="button1_Click"/>
<Button x:Name="button2" Content="Button2" Width="75" Click="button2_Click"/>
</StackPanel>
UC code:
public static readonly RoutedEvent ClickB1Event = EventManager.RegisterRoutedEvent(
"ClickB1", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(UserControl1));
public static readonly RoutedEvent ClickB2Event = EventManager.RegisterRoutedEvent(
"ClickB2", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(UserControl1));
public event RoutedEventHandler ClickB1;
public event RoutedEventHandler ClickB2;
private void button1_Click(object sender, RoutedEventArgs e)
{
if (ClickB1 != null)
{
ClickB1(this, e);
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
if (ClickB2 != null)
{
ClickB2(this, e);
}
}
MainWindow xaml
<local:UserControl1 x:Name="ctrl1" ClickB1="Click1" ClickB2="Click2"/>
MainWindow code:
public MainWindow()
{
InitializeComponent();ctrl1.ClickB1 += Click1; ctrl1.ClickB2 += Click2; } private void Click1(object sender, RoutedEventArgs e) { Debug.WriteLine((sender as UserControl1).Name); } private void Click2(object sender, RoutedEventArgs e) { Debug.WriteLine((sender as UserControl1).Name); }