Raise the PropertyChanged event

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 WpfApplication1" Height="180" Width="260">
    <StackPanel>
        <TextBlock Text="Last Name" VerticalAlignment="Center"/>
        <TextBox Text="{Binding Path=LastName, Mode=TwoWay}"/>
        
        <TextBlock Text="Age" VerticalAlignment="Center"/>
        <TextBox Text="{Binding Path=Age, Mode=TwoWay}"/>
        
        <TextBlock Text="Occupation" VerticalAlignment="Center"/>
        <ComboBox x:Name="cboOccupation" IsEditable="False" HorizontalAlignment="Left"
            Text="{Binding Path=Occupation, Mode=TwoWay}"
            Margin="4" Width="140">
             <ComboBoxItem>Student</ComboBoxItem>
             <ComboBoxItem>Skilled</ComboBoxItem>
             <ComboBoxItem>Professional</ComboBoxItem>
        </ComboBox>
                  
        <TextBlock Margin="4" Text="Description" FontWeight="Bold" FontStyle="Italic" VerticalAlignment="Center"/>
        <TextBlock Margin="4" Text="{Binding Path=Description, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center"/>
        
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System.Windows;
using System.ComponentModel;
namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            
            this.DataContext = new Employee(){
                        LastName = "B",
                        Age = 26,
                        Occupation = "Professional"
            };
        }
    }

    public class Employee : INotifyPropertyChanged
    {
        private string lastName;
        private int age;
        private string occupation;

        public string LastName
        {
            get
            {
                return lastName;
            }
            set
            {
                if(this.lastName != value)
                {
                    this.lastName = value;
                    OnPropertyChanged("LastName");
                    OnPropertyChanged("Description");
                }
            }
        }

        public int Age
        {
            get
            {
                return age;
            }
            set
            {
                if(this.age != value)
                {
                    this.age = value;
                    OnPropertyChanged("Age");
                    OnPropertyChanged("Description");
                }
            }
        }
        
        public string Occupation
        {
            get { return occupation; }
            set
            {
                if (this.occupation != value)
                {
                    this.occupation = value;
                    OnPropertyChanged("Occupation");
                    OnPropertyChanged("Description");
                }
            }
        }

        public string Description
        {
            get
            {
                return string.Format("{0} {1}, ({2})", 
                                     lastName, age, occupation);
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if(this.PropertyChanged != null)
            {

                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    }
}

   
    
     


Get the event sender name

image_pdfimage_print


   
  

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WpfApplication1.Window1"
    Title="Routed Events" Height="400" Width="800">
  
    <Grid Name="contentGrid" Background="Red">
        <Rectangle Name="clickMeRectangle" 
                   Height="70" 
                   Width="70" 
                   Stroke="Black" 
                   Fill="CadetBlue" />
        <Button Name="clickMeButton" 
                Height="23" 
                HorizontalAlignment="Right" 
                VerticalAlignment="Top" 
                Width="70" 
                PreviewMouseDown="Generic_MouseDown" 
                Click="clickMeButton_Click">Click Me</Button>
        <TextBlock Name="outputText" />
    </Grid>
</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;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        private void Generic_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Console.WriteLine(outputText.Text);
            Console.WriteLine(e.RoutedEvent.Name);
            Console.WriteLine(sender.ToString());
            Console.WriteLine(((FrameworkElement)e.Source).Name);
        }

        private void Window_MouseUp(object sender, MouseButtonEventArgs e)
        {
            outputText.Text = outputText.Text;
        }

        private void clickMeButton_Click(object sender, RoutedEventArgs e)
        {
            outputText.Text = "Button clicked:" + outputText.Text;
        }
    }
}

   
    
     


Use Ellipse event delegate

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="Code Events" 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>
        <TextBlock Grid.Column="1">Click!</TextBlock>
      </Grid>
    </Button>
  </Grid>
</Window>


//File:Window.xaml.cs


using System;
using System.Windows;
using System.Diagnostics;

namespace WpfApplication1
{

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

            myEllipse.MouseDown += MouseDownEllipse;
            myEllipse.PreviewMouseDown += PreviewMouseDownEllipse;
        }
        void PreviewMouseDownEllipse(object sender, RoutedEventArgs e)
        { Debug.WriteLine("PreviewMouseDownButton"); }

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

   
    
     


Halting event routing with Handled

image_pdfimage_print


   
  

<Window x:Class="WpfApplication1.HaltingEvents"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Halting Events" Height="300" Width="300">
  <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
    <Button PreviewMouseDown="PreviewMouseDownButton"
    MouseDown="MouseDownButton">

      <Grid PreviewMouseDown="PreviewMouseDownGrid"
            MouseDown="MouseDownGrid">
        <Grid.ColumnDefinitions>
          <ColumnDefinition />
          <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <Canvas PreviewMouseDown="PreviewMouseDownCanvas"
                MouseDown="ButtonDownCanvas"
                Width="20" Height="18" VerticalAlignment="Center">

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

          <Ellipse Canvas.Left="4.5" Canvas.Top="5" Width="2.5" Height="3"
                   Fill="Black" />
          <Ellipse Canvas.Left="11" Canvas.Top="5" Width="2.5" Height="3"
                   Fill="Black" />
          <Path Data="M 5,10 A 3,3 0 0 0 13,10" Stroke="Black" />
        </Canvas>

        <TextBlock Grid.Column="1">Click!</TextBlock>
      </Grid>
    </Button>
  </Grid>
</Window>



//File:Window.xaml.cs

using System;
using System.Windows;
using System.Diagnostics;

namespace WpfApplication1
{
    public partial class HaltingEvents : System.Windows.Window
    {

        public HaltingEvents()
        {
            InitializeComponent();
        }
        void ButtonDownCanvas(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("ButtonDownCanvas");
            e.Handled = true;
        }
        void PreviewMouseDownButton(object sender, RoutedEventArgs e)
        { Debug.WriteLine("PreviewMouseDownButton"); }

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


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

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


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


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

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

   
    
     


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

    }
}