Implementing INotifyPropertyChanged in Silverlight

Are you using binding in xaml? Is it MVVM? So believe me, this post is for you. Just give it a five minute read, you will admire it.

Data binding is one of the coolest features that have ever existed, ever created by the human race. Binding a UI Element's property with a property in the code behind, has the power to do any kind of trick. It's wizardry, in a nutshell. Once the properties are bound, we need to keep notifying the UI if the property's value has been altered in the code. INotifyPropertyChanged is handy for this.

You see, since it is an interface, we need to first implement it. The process is not very tough though. In the new Silverlight project, here is the code of my main page:

publicpartialclassMainPage : UserControl

{

    privatestring _names;

 

    publicstring Names

    {

        get

        {

            return _names;

        }

        set

        {

            _names = value;

        }

    }

 

    public MainPage()

    {

        InitializeComponent();

    }

 

    privatevoid MainPage_OnLoaded(object sender, RoutedEventArgs e)

    {

        Names = "This is the Text";

    }

}

The property "Name" I have here is bound with the textblock in XAML, here is the code:

<UserControlx:Class="PropertyChangedDescribed.MainPage"

 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

  mc:Ignorable="d"

 Loaded="MainPage_OnLoaded"

 x:Name="TestingPropertyChanged"

 d:DesignHeight="300"d:DesignWidth="400">

 <Gridx:Name="LayoutRoot"Background="White">

  <TextBlockText="{Binding Names, ElementName=TestingPropertyChanged}"/>

  </Grid>

</UserControl>

As you can see, the textblock has it's "text" property bound with our code behind's property "Name". Right now, no matter what you set the value of "Name", it will never be reflected onto the UI. So, what we want is, every time we change the value of our property "Name," the text block has its value changed too. In order to do this, we need to implement the interface INotifyPropertyChanged. Here is the modified main page's code to do so:

publicpartialclassMainPage : UserControl, INotifyPropertyChanged

{

    privatestring _names;

 

    publicstring Names

    {

        get

        {

            return _names;

        }

        set

        {

            _names = value;

            OnPropertyChanged("Names");

        }

    }

 

    public MainPage()

    {

        InitializeComponent();

    }

 

    privatevoid MainPage_OnLoaded(object sender, RoutedEventArgs e)

    {

        Names = "This is the Text";

    }

 

    publicevent PropertyChangedEventHandler PropertyChanged;

 

    privatevoid OnPropertyChanged(string propertyName)

    {

        if (this.PropertyChanged != null)

        {

            PropertyChanged(this,new PropertyChangedEventArgs(propertyName));

        }

    }

}

So this is how you can implement INotifyPropertyChanged in Silverlight.

For more articles on Silverlight visit http://www.neelesh-vishwakarma.com