Use Ellipse.AddHandler to add handler to Ellipse objects

image_pdfimage_print


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="AddHandler" Height="300" Width="300">
    <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
        <Button>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>

                <Canvas Width="20" Height="18" VerticalAlignment="Center">

                    <Ellipse x:Name="myEllipse" Canvas.Left="1" Canvas.Top="1" Width="16" Height="16"
                   Fill="Yellow" Stroke="Black" />
                </Canvas>

            </Grid>
        </Button>
    </Grid>
</Window>
//File:Window.xaml.cs


using System;
using System.Windows;
using System.Diagnostics;
using System.Windows.Shapes;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class Window1 : System.Windows.Window
    {
        public Window1()
        {
            InitializeComponent();

            myEllipse.AddHandler(Ellipse.MouseDownEvent,new MouseButtonEventHandler(MouseDownEllipse));
            myEllipse.AddHandler(Ellipse.PreviewMouseDownEvent,new MouseButtonEventHandler(PreviewMouseDownEllipse));
        }
       
        void PreviewMouseDownEllipse(object sender, RoutedEventArgs e){ 
            Debug.WriteLine("PreviewMouseDownEllipse"); 
        }

        void MouseDownEllipse(object sender, RoutedEventArgs e){ 
            Debug.WriteLine("MouseDownEllipse"); 
        }
    }
}

   
    
     


Get event sender from event

image_pdfimage_print


   
  
<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);
    }
  }
}

   
    
     


Handle the ContentRendered event

image_pdfimage_print


   
  

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

    }
}

   
    
     


Find source element of an element in event handler by casint

image_pdfimage_print


   
  


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

   
    
     


Add an event handler to an element using code

image_pdfimage_print


   
  

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

   
    
     


Handles the Click event on the UniformGrid

image_pdfimage_print


   
  


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

   
    
     


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

image_pdfimage_print


   
  


<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);
        }
    }
}