Suppose you want to create simple event handler of any control. means you want if you click on Button control then some specific task should be performed, for it we use event handler and in this case we use clixk event of the button control.
Here articles describe ways to create simple event handlers in the WPF Designer.
How to create a default event handler:
you can create default event handler just by double click on the control.after that the code-behind file is opened and the cursor is positioned in the default event handler.
How to create an event handler in XAML
following are the steps for creating event handler in xaml.(we are using button click event on here)
1. locate button element in xaml.
2. Write the event name that you want to handle in the start tag for the element. Suppose if you want to use Click event of the button then you can write Click then an IntelliSense list appears with the available events. you can choose from this list.
The following XAML specifies a Click event handler for a Button control.
<Window x:Class="Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="252" Width="300" Name="Window1"> <Grid Height="221" Width="300"> <Button Name="Button1" Width="75" HorizontalAlignment="Left" Margin="92,90,0,108" Click="Button1_Click">Button</Button> </Grid> </Window>
3. if you want to use this event in code behind file then you can right-click the event or event handler name and select the Navigate to Event Handler option in xaml view, then The code-behind file is opened and the cursor is positioned in the selected event handler. The following code shows example event handlers.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) MessageBox.Show("Welcome") End Sub
How to create an event handler by using the Code Editor drop-down lists
1. Open the Class Name drop-down list In the Code Editor.
2. Select the control or type that you want to create an event handler for.
3. Open the Method Name drop-down list.
4. Select the event that you want create an event handler for.
An event handler is created and the cursor is positioned in the event handler.