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 17:52]
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));
     }     }
  
Line 378: Line 380:
 === Example of Custom RoutedEvent === === Example of Custom RoutedEvent ===
  
-UserNotification logic is composed of a UserNotification component that triggers the event, and a UserNotificationArea component to handle and display the event.  This last component is usually place in any parent control that needs to respond to user notifications triggered in child controls.+UserNotification logic is composed of a u''cUserNotification'' component that triggers the event, and a ''ucUserNotificationArea'' component to handle and display the event.  This last component is usually placed in any parent control that needs to respond to user notifications triggered in child controls.  There are no direct event subscriptions other than the AddHandle() performed on the parent control.  It portrays how notifications can be triggers in any child controls, and then handled as an event at some ancestor control without having to know anything about the child control that triggered the event.  This mechanism only works for VisualTree components.  The alternative is to use CLR events, but then there must be direct subscriptions to events from the parent to the child control in order to handle those events, something we are avoiding here with this mechanism.
  
 UserControl for UserNotification event trigger. UserControl for UserNotification event trigger.
Line 797: Line 799:
 </code> </code>
  
-UserControl placed in parent control of the control that triggers a message:+UserControl placed in control that triggers a message: 
 +<code xml> 
 +<toolscontrols:ucUserNotification x:Name="msgUserNotification" IsNotificationAreaVisible="False"/> 
 +</code> 
 + 
 +UserControl placed in parent control of the control that triggers a message.  This is where the notification will be displayed:
 <code xml> <code xml>
 <!--User Notification Area--> <!--User Notification Area-->
Line 805: Line 812:
 Method to display message:             Method to display message:            
 <code csharp> <code csharp>
 +///----------------------------------------------------------------------------------------
 +/// <summary>
 +/// Loaded event handler for user control.
 +/// </summary>
 +/// <param name="sender"></param>
 +/// <param name="e"></param>
 +///----------------------------------------------------------------------------------------
 +private void UserControl_Loaded(object sender, RoutedEventArgs e)
 +{
 +    . . .
 +
 +    // Subscribe to Events
 +    AddHandler(ucUserNotification.OnUserNotificationRoutedEvent, new ucUserNotification.UserNotificationRoutedEventHandler(UserNotification));
 +}
 +
 ///---------------------------------------------------------------------------------------- ///----------------------------------------------------------------------------------------
 /// <summary> /// <summary>