Click
event
<Button x:Name="button1" Click="button1_Click">Press Me</Button>
button1.Click += new RoutedEventHandler(button1_Click);
private void button1_Click(object sender, RoutedEventArgs e) { label1.Content = "Button pressed!"; }
<GroupBox Name="myGroupBox" Header="Bubbling Example" MouseLeftButtonUp="MyCallback"> <Label x:Name="myLabel" MouseLeftButtonUp="MyCallback">Click Me</Label> </GroupBox>
private void MyCallback(object sender, MouseButtonEventArgs e) { // Label notified of event first, then GroupBox }
<GroupBox Name="myGroupBox" Header="Tunneling Example" PreviewMouseLeftButtonUp="MyCallback"> <Label x:Name="myLabel" PreviewMouseLeftButtonUp="MyCallback">Click Me</Label> </GroupBox>
private void MyCallback(object sender, MouseButtonEventArgs e) { // GroupBox notified of event first, then Label }
Note: All tunneling events start with "Preview"
sender
- object where the handler was invoked
private void button_Click(object sender, RoutedEventArgs e) { // Using a shared handler, determine which button was pressed Button srcButton = e.Source as Button; if (srcButton.Name == "button1") myLabel.Content = "button 1 was pressed"; else myLabel.Content = "button 2 was pressed"; }
RoutedEventArgs
- contains state info and event data associated
with the event
// KeyEventArgs inherits from RoutedEventArgs private void Grid1_PreviewKeyDown(object sender, KeyEventArgs e) { // Stop event from tunneling down further e.Handled = true; }
<Window x:Class="HelloWpf.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="190" Width="300"> <Window.CommandBindings> <CommandBinding Command="Close" Executed="CloseCommand_Executed" /> </Window.CommandBindings> <Grid> <ToolBar HorizontalAlignment="Left" VerticalAlignment="Top"> <Button Command="Copy">Copy</Button> <Button Command="Paste">Paste</Button> <Button Command="Close">Exit</Button> </ToolBar> <RichTextBox HorizontalAlignment="Left" Height="128" Margin="0,32,0,0" VerticalAlignment="Top" /> </Grid> </Window>
// Close command handler private void CloseCommand_Executed(object sender, ExecutedRoutedEventArgs e) { Application.Current.Shutdown(); }
<CommandBinding Command="Close" Executed="CloseCommand_Executed" CanExecute="CloseCommand_CanExecute" />
// Don't allow closing if the file is not saved private void CloseCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = fileIsSaved; // bool }
<Window x:Class="CustomCommandDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:CustomCommandDemo" ... > <Window.CommandBindings> <CommandBinding Command="{x:Static local:MainWindow.MyCommand}" Executed="MyCommand_Executed" CanExecute="MyCommand_CanExecute" /> </Window.CommandBindings>
public partial class MainWindow : Window { // Make command accessible to XAML public static RoutedCommand MyCommand = new RoutedCommand(); ... private void MyCommand_Executed(object sender, ExecutedRoutedEventArgs e) { // Do something } private void MyCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e) { E.CanExecute = true; } }