Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
swdev:dotnet:events [2012/11/05 17:43] smayr [Resources] |
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. | + | In WPF, you also have access to Routed Events, which have additional advantages. |
RoutedEvents are particularly useful if the listener doesn' | RoutedEvents are particularly useful if the listener doesn' | ||
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 | + | public static readonly RoutedEvent |
- | " | + | " |
| | ||
| | ||
Line 181: | Line 181: | ||
// Provide CLR property wrapper for the routed event | // Provide CLR property wrapper for the routed event | ||
- | public event RoutedEventHandler | + | public event RoutedEventHandler |
{ | { | ||
- | add { AddHandler(MyRoutedEvent, value); } | + | add { AddHandler(OnMyActionEvent |
- | remove { RemoveHandler(MyRoutedEvent, value); } | + | remove { RemoveHandler(OnMyActionEvent |
} | } | ||
// Method to raise event | // Method to raise event | ||
- | public void RaiseMyRoutedEvent() | + | public void RaiseMyActionEvent() |
{ | { | ||
- | | + | |
} | } | ||
Line 310: | Line 310: | ||
// Attach menu event handler | // Attach menu event handler | ||
- | popupmnu1.MenuItemSelected += new RoutedEventHandler(ucPopupMnu_MenuItemSelected); | + | |
+ | // or | ||
+ | AddHandler(ucPopupMnu.MenuItemSelectedEvent, | ||
} | } | ||
Line 376: | Line 378: | ||
} | } | ||
</ | </ | ||
- | == Example of Custom RoutedEvent == | + | === Example of Custom RoutedEvent |
- | ucUserNotification.xaml: | + | UserNotification logic is composed of a u'' |
+ | |||
+ | UserControl for UserNotification event trigger. | ||
+ | '' | ||
<code xml> | <code xml> | ||
< | < | ||
Line 393: | Line 398: | ||
</ | </ | ||
- | ucUserNotification.xaml.cs: | + | '' |
<code csharp> | <code csharp> | ||
using System; | using System; | ||
Line 567: | Line 572: | ||
UserControl to display user notification. | UserControl to display user notification. | ||
+ | '' | ||
<code xml> | <code xml> | ||
< | < | ||
Line 606: | Line 612: | ||
</ | </ | ||
- | ucUserNotificationArea.xaml.cs: | + | '' |
<code csharp> | <code csharp> | ||
using System; | using System; | ||
Line 793: | Line 799: | ||
</ | </ | ||
+ | UserControl placed in control that triggers a message: | ||
+ | <code xml> | ||
+ | < | ||
+ | </ | ||
+ | UserControl placed in parent control of the control that triggers a message. | ||
+ | <code xml> | ||
+ | <!--User Notification Area--> | ||
+ | < | ||
+ | </ | ||
+ | |||
+ | Method to display message: | ||
+ | <code csharp> | ||
+ | /// | ||
+ | /// < | ||
+ | /// Loaded event handler for user control. | ||
+ | /// </ | ||
+ | /// <param name=" | ||
+ | /// <param name=" | ||
+ | /// | ||
+ | private void UserControl_Loaded(object sender, RoutedEventArgs e) | ||
+ | { | ||
+ | . . . | ||
+ | |||
+ | // Subscribe to Events | ||
+ | AddHandler(ucUserNotification.OnUserNotificationRoutedEvent, | ||
+ | } | ||
+ | |||
+ | /// | ||
+ | /// < | ||
+ | /// Set user notification in the display area. | ||
+ | /// </ | ||
+ | /// <param name=" | ||
+ | /// <param name=" | ||
+ | /// | ||
+ | private void UserNotification(object source, UserNotificationRoutedEventArgs args) | ||
+ | { | ||
+ | // Display message for 4 sec | ||
+ | ctrlUserNotificationArea.SetTextMessage(args.Message, | ||
+ | } | ||
+ | </ | ||
== Resources == | == Resources == | ||
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// |