I have a UserControl which acts as a custom button, but since I changed my namespaces around the routed click event no longer works. Essentially my namespaces look something like this:

UI
UI.Controls
UI.Pages

I have a UI.Controls.CustomButton which has a routed click event implemented something like:

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);
}

And I am trying to use it from within a UI.Pages.SomePage like so:

XAML

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

C#

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

When I debug the application I see that RaiseEvent inside my UI.Controls.CustomButton is being hit, but the NewButton_Click inside my UI.Pages.SomePage is never called.

This was working when everything was in the same namespace... am I missing something obvious? Thanks!

有帮助吗?

解决方案

RoutedEventArgs newEventArgs = new RoutedEventArgs(Button.ClickEvent);

should be:

RoutedEventArgs newEventArgs = new RoutedEventArgs(CustomButton.ClickEvent);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top