Monday, July 18, 2011

INotifyPropertyChanged Interface in WPF

In WPF data binding, target property must be a Dependency Property and source property must be able to notify target object when source property value has changed. Source object can be Dependency Property or normal CLR property of your class. As you might aware that Dependency Property has built in notification mechanism. So when you bind dependency property as source object then you don’t need to worry about notification but if you bind normal CLR property as source then you need take care to provide notification to the target object.

How to implement INotifyPropertyChanged?

INotifyPropertyChanged interface provides notification to clients (targe object) when source property has changed in data binding. This interface is available in System.ComponentModel namespace. For change notification to target objects, need to raise PropertyChanged event from setter block of property.

Below is the simple and basic example which shows how to implement INotifyPropertyChanged interface and how to bind properties to UI element.

<Window x:Class="WpfApplication1.NotifyPropertyChange"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="Notify Property Change Sample"
        Height="130" Width="300">
<Window.Resources>
    <local:StudentInfo FirstName="Mitesh"
                       LastName="Sureja"
                       Age="29"
                       x:Key="MyStudentInfo"/>
</Window.Resources>
<Grid DataContext="{StaticResource MyStudentInfo}">
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="0.15*"/>
        <RowDefinition Height="0.15*"/>
    </Grid.RowDefinitions>
    <TextBlock Text="Student Name:" Margin="5"
            Grid.Row="0" Grid.Column="0"
            VerticalAlignment="Center" />
    <TextBox Name="StudentName" Height="30" Margin="5"
             Text="{Binding Path=FullName, Mode=OneWay}"
             Grid.Row="0" Grid.Column="1" />
    <TextBlock Text="Student Age:" Margin="5"
               Grid.Row="1" Grid.Column="0"
               VerticalAlignment="Center"/>
    <TextBox Name="StudentAge" Height="30" Margin="5"
             Text="{Binding Path=Age}"
             Grid.Row="1" Grid.Column="1" />
</Grid>
</Window>

    public class StudentInfo : INotifyPropertyChanged
    {
        private string firstname;
        public string FirstName
        {
            get
            {
                return firstname;
            }
            set
            {
                firstname = value;
                OnPropertyChanged("FirstName");
                OnPropertyChanged("FullName");
            }
        }
        private string lastName;
        public string LastName
        {
            get
            {
                return lastName;
            }
            set
            {
                lastName = value;
                OnPropertyChanged("LastName");
                OnPropertyChanged("FullName");
            }
        }
        private string fullName;
        public string FullName
        {
            get
            {
                return FirstName + " " + LastName;
            }
        }
        private int age;
        public int Age
        {
            get
            {
                return age;
            }
            set
            {
                age = value;
                OnPropertyChanged("Age");
            }
        }

        //Implementation of INotifyPropertyChanged Interface
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }












Related Links 


2 comments: