Skip to content

Instantly share code, notes, and snippets.

@wi7a1ian
Last active March 18, 2019 09:25
Show Gist options
  • Save wi7a1ian/afe4e9b8091fbdabee8c79211bec6e80 to your computer and use it in GitHub Desktop.
Save wi7a1ian/afe4e9b8091fbdabee8c79211bec6e80 to your computer and use it in GitHub Desktop.
Example of RoutedEvent #wpf #csharp
// Register the routed event
public static readonly RoutedEvent SelectedEvent =
EventManager.RegisterRoutedEvent( "Selected", RoutingStrategy.Bubble,
typeof(RoutedEventHandler), typeof(MyCustomControl));
// .NET wrapper
public event RoutedEventHandler Selected
{
add { AddHandler(SelectedEvent, value); }
remove { RemoveHandler(SelectedEvent, value); }
}
// Raise the routed event "selected"
RaiseEvent(new RoutedEventArgs(MyCustomControl.SelectedEvent));
<Window x:Class = "WPFRoutedEvents.MainWindow"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "MainWindow" Height = "450" Width = "604" MyCustomControl.Selected = "Window_Selected" >
<Grid>
<StackPanel Margin = "20" MyCustomControl.Selected = "StackPanel_Selected">
<local:MyCustomControl Selected = "MyCustomControl_Selected" />
</StackPanel>
</Grid>
</Window>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment