Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
swdev:dotnet:events [2012/11/05 18:04]
smayr [Example of Custom RoutedEvent]
swdev:dotnet:events [2015/06/15 15:28] (current)
ajdavis [Routed Events]
Line 164: Line 164:
 == Routed Events == == Routed Events ==
  
-In WPF, you also have access to Routed Events, which have additional advantages.  Events can "tunnel" or "bubble" to other components in the visual tree.+In WPF, you also have access to Routed Events, which have additional advantages.  Events can "tunnel down" or "bubble up" to other components in the visual tree.
  
 RoutedEvents are particularly useful if the listener doesn't have a direct reference to the source of the event. For example you have a container control which must react to a specific event, but the container will not always know exactly which controls inside it can/will throw it. The controls can be several levels deep, or created by a template. – Source: [[http://stackoverflow.com/questions/2536950/c-wpf-routedevent-in-wpf-class-that-isnt-a-uielement|Bubblewrap@Stackoverflow.com]] RoutedEvents are particularly useful if the listener doesn't have a direct reference to the source of the event. For example you have a container control which must react to a specific event, but the container will not always know exactly which controls inside it can/will throw it. The controls can be several levels deep, or created by a template. – Source: [[http://stackoverflow.com/questions/2536950/c-wpf-routedevent-in-wpf-class-that-isnt-a-uielement|Bubblewrap@Stackoverflow.com]]
Line 174: Line 174:
 Writing a custom routed event is similar to writing a dependency property, Writing a custom routed event is similar to writing a dependency property,
 <code csharp> <code csharp>
-public static readonly RoutedEvent MyRoutedEvent = EventManager.RegisterRoutedEvent( +public static readonly RoutedEvent OnMyActionEvent = EventManager.RegisterRoutedEvent( 
-                               "MyRoutedEvent",            // Name of the custom routed event+                               "OnMyAction",               // Name of the custom routed event
                                RoutingStrategy.Bubble,     // The routing strategy                                RoutingStrategy.Bubble,     // The routing strategy
                                typeof(RoutedEventHandler), // Type of the event handler                                typeof(RoutedEventHandler), // Type of the event handler
Line 181: Line 181:
  
 // Provide CLR property wrapper for the routed event // Provide CLR property wrapper for the routed event
-public event RoutedEventHandler MyEvent+public event RoutedEventHandler OnMyAction
 { {
-    add { AddHandler(MyRoutedEvent, value); } +    add { AddHandler(OnMyActionEvent , value); } 
-    remove { RemoveHandler(MyRoutedEvent, value); }+    remove { RemoveHandler(OnMyActionEvent , value); }
 } }
  
 // Method to raise event // Method to raise event
-public void RaiseMyRoutedEvent()+public void RaiseMyActionEvent()
 { {
-  RaiseEvent(new RoutedEventArgs(MyControl.MyRoutedEvent));+    RaiseEvent(new RoutedEventArgs(MyControl.OnMyActionEvent ));
 } }
  
Line 310: Line 310:
  
         // Attach menu event handler         // Attach menu event handler
-        popupmnu1.MenuItemSelected += new RoutedEventHandler(ucPopupMnu_MenuItemSelected);+        //popupmnu1.MenuItemSelected += new RoutedEventHandler(ucPopupMnu_MenuItemSelected); 
 +        // or 
 +        AddHandler(ucPopupMnu.MenuItemSelectedEvent, new RoutedEventHandler(ucPopupMnu_MenuItemSelected));
     }     }