Shalvin Interests

Tuesday, July 19, 2011

INotifyPropertyChanged Interface

A change-notification handler notifies a binding target that a change has been
made. This enables a target to automatically respond to changes. Dependency properties
already have this feature built in, but CLR properties don’t. If you want your CLR
properties to broadcast their changes, you must implement the INotifyProperty-
Changed interface.

INotifyPropertyChanged interface belongs to System.ComponentModel namespace.

using System.ComponentModel;

namespace INotifyProperyChangeShalvin
{
    public class Speaker : INotifyPropertyChanged
    {
        private string mSpeakerName;
            public string SpeakerName
            {
                get { return mSpeakerName; }
                set
                {
                    mSpeakerName = value;
                    FirePropertyChanged("SpeakerName");
                }
            }
            public event PropertyChangedEventHandler PropertyChanged;


            void FirePropertyChanged(string property)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this,
                        new PropertyChangedEventArgs(property));
                }
            }
        }
    }




private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    Speaker s = new Speaker { SpeakerName = "Shalvin" };
    LayoutRoot.DataContext = s;
}


<Grid x:Name="LayoutRoot" Background="White">
        <TextBox Height="23" HorizontalAlignment="Left" Margin="120,47,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Text="{Binding SpeakerName, Mode=TwoWay}" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="120,147,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" Text="{Binding SpeakerName, Mode=TwoWay}" />
</Grid>

1 comment:

  1. The first part of the implementation is a new I Auto Notify Property Changed interface that extends the I Notify Property Changed interface with one new method.
    http://www.dapfor.com/en/net-suite/net-grid/features/performance

    ReplyDelete