WPF未从用户控件接收路由事件

时间:2012-08-14 14:58:13

标签: c# wpf user-controls routed-events

我有UserControl作为自定义按钮,但由于我更改了路由点击事件周围的名称空间不再有效。基本上我的命名空间看起来像这样:

UI
UI.Controls
UI.Pages

我有一个UI.Controls.CustomButton,其路由点击事件的实现类似于:

public static readonly RoutedEvent ClickEvent = EventManager.RegisterRoutedEvent(
     "Click", 
     RoutingStrategy.Bubble, 
     typeof(RoutedEventHandler), 
     typeof(CustomButton));

public event RoutedEventHandler Click
{
     add { AddHandler(ClickEvent, value); }
     remove { RemoveHandler(ClickEvent, value); }
}

private void Border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
     Border_MouseEnter(sender, e);
     RaiseClickEvent();
}

private void RaiseClickEvent()
{
     RoutedEventArgs newEventArgs = new RoutedEventArgs(Button.ClickEvent);
     RaiseEvent(newEventArgs);
}

我正试图在UI.Pages.SomePage之内使用它:

XAML

<controls:CustomButton Text="New..." Click="NewButton_Click"/>

C#

public void NewButton_Click(object sender, RoutedEventArgs e)
{
    //do something here
}

当我调试应用程序时,我发现RaiseEvent内的UI.Controls.CustomButton被点击了,但我的NewButton_Click内的UI.Pages.SomePage从未被调用过。

当一切都在同一名称空间中时,这是有效的......我错过了一些明显的东西吗?谢谢!

1 个答案:

答案 0 :(得分:8)

RoutedEventArgs newEventArgs = new RoutedEventArgs(Button.ClickEvent);

应该是:

RoutedEventArgs newEventArgs = new RoutedEventArgs(CustomButton.ClickEvent);