Check handler event sender


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="150" Width="250">
    <StackPanel>
        <Popup AllowsTransparency="True" Height="100" HorizontalOffset="1cm" Name="myPopup" 
                   Placement="Right" StaysOpen="True" Width="200" >
            <Border BorderBrush="Black" BorderThickness="2">
                <DockPanel Background="White" LastChildFill="True">
                    <TextBlock Background="AliceBlue" DockPanel.Dock="Top" 
                                   FontSize="16" HorizontalAlignment="Stretch" 
                                   Margin="5" Text="A WPF Popup" />
                        <Button Click="btnClosePopup_Click" Content="Close" 
                                DockPanel.Dock="Bottom" Margin="5" 
                                HorizontalAlignment="Right" MaxHeight="23"/>
                        <Image DockPanel.Dock="Top" Margin="5" 
                               Source="c:image.gif" />
                    </DockPanel>
            </Border>
        </Popup>
        <StackPanel>
            <StackPanel.Resources>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="Margin" Value="2" />                        
                    <EventSetter Event="Click" Handler="btnShowPopup_Click" />
                </Style>
            </StackPanel.Resources>
            <Button Content="Show Popup" Name="btnShowPopup" />
        </StackPanel>
    </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void btnClosePopup_Click(object sender, RoutedEventArgs e)
        {
            myPopup.IsOpen = false;
        }

        private void btnShowPopup_Click(object sender, RoutedEventArgs e)
        {
            if (sender == btnShowPopup){
            
                myPopup.IsOpen = true;
            } 
        }
    }
}

   
    
     


Mark the text control as being changed to prevent any text content or selection changed events


   
  


<Window 
  x:Class="WpfApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Window1" Height="600" Width="800">
  <DockPanel>
    <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal">
      <TextBox x:Name="tbxInsertionText" Width="200" Margin="5,0" />
      <Button DockPanel.Dock="Bottom" Content="Insert" Click="btnInsert_Click"/>
    </StackPanel>
    <RichTextBox x:Name="rtbTextContent" />
  </DockPanel>
</Window>
//File:Window.xaml.cs
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void btnInsert_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(tbxInsertionText.Text))
            {
                return;
            }
            rtbTextContent.BeginChange();
            if (rtbTextContent.Selection.Text != string.Empty)
            {
                rtbTextContent.Selection.Text = string.Empty;
            }
            TextPointer tp = rtbTextContent.CaretPosition.GetPositionAtOffset(0, LogicalDirection.Forward);
            rtbTextContent.CaretPosition.InsertTextInRun(tbxInsertionText.Text);
            rtbTextContent.CaretPosition = tp;
            rtbTextContent.EndChange();
            Keyboard.Focus(rtbTextContent);
        }
    }
}

   
    
     


Handles the Click event on the UniformGrid


   
  


<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="150" Width="300">
    <UniformGrid Columns="2" Rows="2" ButtonBase.Click="UniformGrid_Click" 
                 MouseDown="UniformGrid_MouseDown">
        <Button Content="Button" MaxHeight="25" MaxWidth="70" Name="Button"/>
        <Label Background="LightBlue" Content="Label" Name="Label"
               HorizontalContentAlignment="Center" 
               MaxHeight="25" MaxWidth="100"/>
        <TextBlock Background="Turquoise" Padding="25,7" Text="TextBlock" 
               HorizontalAlignment="Center" VerticalAlignment="Center"
               Name="TextBlock"/>
        <Canvas MouseDown="Canvas_MouseDown">
            <Rectangle Canvas.Top="15" Canvas.Left="20" Fill="Aqua" 
                Height="25" Width="100" Name="Rectangle"/>
            <TextBlock Canvas.Top="20" Canvas.Left="45" Text="Rectangle" 
                   IsHitTestVisible="False"/>            
        </Canvas>
    </UniformGrid>
</Window>
//File:Window.xaml.cs
using System.Windows;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        // Handles the MouseDown event on the Canvas.
        private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement fe = e.OriginalSource as FrameworkElement;

            MessageBox.Show("Mouse Down on " + fe.Name, "Canvas");
        }

        // Handles the Click event on the UniformGrid.
        private void UniformGrid_Click(object sender, RoutedEventArgs e)
        {
            FrameworkElement fe = e.OriginalSource as FrameworkElement;

            MessageBox.Show("Mouse Click on " + fe.Name, "Uniform Grid");
        }

        // Handles the MouseDown event on the UniformGrid.
        private void UniformGrid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement fe = e.OriginalSource as FrameworkElement;

            MessageBox.Show("Mouse Down on " + fe.Name, "Uniform Grid");
        }
    }
}

   
    
     


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!!";
        }
    }
}

   
    
     


Find source element of an element in event handler by casint


   
  


<StackPanel
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="WpfApplication1.RoutedEventSource">
  <StackPanel.Resources>
    <Style TargetType ="{x:Type Button}">
      <Setter Property="Height" Value="30"/>
      <Setter Property="Width" Value="100"/>
      <Setter Property="HorizontalAlignment" Value="Left"/>
    </Style>
  </StackPanel.Resources>
  <Button Click="HandleClick">Button 1</Button>
  <Button Click="HandleClick">Button 2</Button>
  <Button Click="HandleClick">Button 3</Button>    
</StackPanel>

//File:Window.xaml.cs
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1 {
    public partial class RoutedEventSource {
        void HandleClick(object sender, RoutedEventArgs e)
        {
            Button srcButton = e.Source as Button;
      srcButton.Width = 200;
        }
    }
}

   
    
     


Handle the ContentRendered event


   
  

<Window x:Class="ContentRendered.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ContentRendered" Height="300" Width="300" ContentRendered="OnContentRendered">
    <Grid>
        
    </Grid>
</Window>

//File:Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


namespace ContentRendered
{
    public partial class Window1 : System.Windows.Window
    {

        public Window1()
        {
            InitializeComponent();
        }

        private void OnContentRendered(object sender, EventArgs e)
        {
            MessageBox.Show("Content Rendered Event Fired!");
        }

    }
}

   
    
     


Get event sender from event


   
  
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:d="http://schemas.microsoft.com/expression/interactivedesigner/2006"
  mc:Ignorable="d" Background="#FFFFFFFF" x:Name="DocumentRoot"
  x:Class="InputExamples.EventHandling" Width="640" Height="480">
  <Grid.Resources>
    <Storyboard x:Key="OnLoaded"/>
  </Grid.Resources>
  
  <Grid.ColumnDefinitions>
    <ColumnDefinition/>
  </Grid.ColumnDefinitions>
  <Grid.RowDefinitions>
  <RowDefinition/>
  </Grid.RowDefinitions>
  <Button HorizontalAlignment="Left" 
          VerticalAlignment="Top" 
          Width="106" 
          Height="28" 
          x:Name="ClickButton1" 
          Content="Click Me!" 
          Click="ClickHandler" PreviewMouseUp="ButtonMouseUpHandler"/>
  <Button d:LayoutOverrides="Width, Height" 
          HorizontalAlignment="Left" 
          VerticalAlignment="Top" 
          Margin="20" 
          Width="106" 
          Height="28" 
          x:Name="ClickButton2" 
          Content="Click Me!" 
          Click="ClickHandler" 
          PreviewMouseUp="ButtonMouseUpHandler"/>
</Grid>

//File:Window.xaml.cs
using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;

namespace InputExamples
{
  public partial class EventHandling
  {
    public EventHandling()
    {
      this.InitializeComponent();
    }
    
    private void ClickHandler(object sender, RoutedEventArgs e)
    {
          Button clicked = e.Source as Button;
          MessageBox.Show(String.Format("{0} was clicked!", clicked.Name));
    }

    private void ButtonMouseUpHandler(object sender, MouseButtonEventArgs e)
    {
          Button clicked = sender as Button;
          clicked.Content = String.Format("{0} clicked", clicked.Name);
    }
  }
}