Add an event handler to an element using code : Event « Windows Presentation Foundation « C# / C Sharp






Add an event handler to an element using code

Add an event handler to an element using code
   

<StackPanel
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="WpfApplication1.RoutedEventAddRemoveHandler" 
  Name="root">
    <StackPanel.Resources>
      <Style TargetType="{x:Type Button}">
        <Setter Property="Height" Value="20"/>
        <Setter Property="Width" Value="250"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
      </Style>
    </StackPanel.Resources>
  <TextBlock Name="text1">Clicking the button below</TextBlock>
  <Button Name="b1" Click="MakeButton">Make new button and add handler to it</Button>
</StackPanel>
//File:Window.xaml.cs

using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1 {
    public partial class RoutedEventAddRemoveHandler {
        
        void MakeButton(object sender, RoutedEventArgs e)
        {
            Button b2 = new Button();
            b2.Content = "New Button";
            // You can remove the event handler using "-=" syntax rather than "+=".
            b2.Click  += new RoutedEventHandler(Onb2Click);
            root.Children.Insert(root.Children.Count, b2);
            DockPanel.SetDock(b2, Dock.Top);
            text1.Text = "click me...";
            b1.IsEnabled = false;
        }
        void Onb2Click(object sender, RoutedEventArgs e)
        {
            text1.Text = "New Button (b2) Was Clicked!!";
        }
    }
}

   
    
    
  








Related examples in the same category

1.Cast event sender to a controlCast event sender to a control
2.Press F1 to get helpPress F1 to get help
3.Raise the PropertyChanged eventRaise the PropertyChanged event
4.Get the event sender nameGet the event sender name
5.Use Ellipse event delegateUse Ellipse event delegate
6.Halting event routing with HandledHalting event routing with Handled
7.Use Ellipse.AddHandler to add handler to Ellipse objectsUse Ellipse.AddHandler to add handler to Ellipse objects
8.Get event sender from eventGet event sender from event
9.Handle the ContentRendered eventHandle the ContentRendered event
10.Find source element of an element in event handler by casintFind source element of an element in event handler by casint
11.Handles the Click event on the UniformGridHandles the Click event on the UniformGrid
12.Mark the text control as being changed to prevent any text content or selection changed eventsMark the text control as being changed to prevent any text content or selection changed events
13.Check handler event senderCheck handler event sender
14.Generate Click Events Repeatedly While a Button Is ClickedGenerate Click Events Repeatedly While a Button Is Clicked
15.Raise the ScrollChanged event of a ScrollViewerRaise the ScrollChanged event of a ScrollViewer
16.Event firing sequenceEvent firing sequence
17.ToolBar and event handlerToolBar and event handler
18.Cancel event by setting CanExecute and Handled to falseCancel event by setting CanExecute and Handled to false
19.Activated and Deactivated events monitors the activation status of an application.
20.Event sender, event source and event original sourceEvent sender, event source and event original source
21.Attach a double click to an object