I have a custom Button class that always performs the same action when clicked (opening a specific window). I am adding a Click event that can be assigned to a XAML button, just like a regular button.
When clicked, I want to execute the Click event handler if it has been assigned, otherwise I want to execute the default action. The problem is that there seems to be no way to check if any handlers are added to the event.
I thought a null event check would do this:
if (Click == null)
{
DefaultClickAction();
}
else
{
RaiseEvent(new RoutedEventArgs(ClickEvent, this));;
}
... but this does not compile. The compiler tells me that I can do nothing but + = or - = for an event outside the defining class, an event, although I am trying to make this INSIDE check the defining class.
, , , . - .
:
public class MyButtonClass : Control
{
public static readonly RoutedEvent ClickEvent =
EventManager.RegisterRoutedEvent("Click",
RoutingStrategy.Bubble,
typeof(RoutedEventHandler),
typeof(MyButtonClass));
public event RoutedEventHandler Click
{
add { ClickHandlerCount++; AddHandler(ClickEvent, value); }
remove { ClickHandlerCount--; RemoveHandler(ClickEvent, value); }
}
private int ClickHandlerCount = 0;
private Boolean ClickHandlerExists
{
get { return ClickHandlerCount > 0; }
}
}