I have declared an event in my UserControl class Main:
public static readonly RoutedEvent CloseEvent = EventManager.RegisterRoutedEvent(
"CloseClick", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Main));
public event RoutedEventHandler CloseClick
{
add { AddHandler(CloseEvent, value); }
remove { RemoveHandler(CloseEvent, value); }
}
This code should be valid since I’ve used in other classes. The problem is when Im trying to listen to the event in another class.
UserControl content;
if (value == "main")
{
content = new Main();
content.CloseClick += new RoutedEventHandler(closeClick);
} else {
...
...
}
MasterPage.addContent(content);
I want “content” to be of any type derived from UserControl.
If I declare “content” as type “Main” the event is found but If its declared as the BaseClass “UserControl” everything but the event works. Am I missing something?
Thats how it should work right. I mean
Mainis the correct type for the source of that event and notUserControl(which can be anything).If you want an event that is raised for any userControl then create a Attached Routed Events. They can be raised from any DependencyObject (i.e.
Mainor baseUserControlor both).