SIGN UP MEMBER LOGIN:    
ARTICLE

C# and XAML within a Silverlight 2 context - INotifyPropertyChanged implementation: Part IV

Posted by Bechir Bejaoui Articles | Learn .NET December 18, 2008
In this article, I will expose the implementation technique of the INotifiedPropertyChanged interface as a part of the article How does XAML interact with the C# code
Reader Level:
Download Files:
 

To test the class or the instantiated object behavior we add some binding objects, a person variable and a method that implements the binding process with the targeted XAML side, to see how do this is implemented with more details then see the previous articles how does C# code behind interact with an XAML interface within a Silverlight 2 context? - Binding process: Part II and Part III.

        Binding FirstNameBind;
        Binding LastNameBind;
        Binding PseudoNameBind;
        Person oPerson;

        public Page()
        {
            InitializeComponent();
            BindingIssue();
        }
        public void BindingIssue()
        {
           //The person is defined in the C# side this once

            oPerson = new Person("Bejaoui","Bechir","Yougethen");

             /* Initiate the Binding value and set them each one to
             * it's appropriate peoperty */

            FirstNameBind = new Binding("FirstName");
            LastNameBind = new Binding("LastName");
            PseudoNameBind = new Binding("PseudoName");

            /* Set the source of each binding source to
             * the person  instance */

            FirstNameBind.Source = oPerson;
            LastNameBind.Source = oPerson;
            PseudoNameBind.Source = oPerson;
            //Get the collection of the textblocs
           UIElementCollection TextBlocks = LayoutRoot.Children;

             /* As 0,1 and 2 are reserved to the labels, namely first name
             , last namr and pseudo name, we should start from index 3*/

            TextBlock txtFirstName = TextBlocks[3] as TextBlock;
            TextBlock txtLastName = TextBlocks[4] as TextBlock;
           TextBlock txtPseudoName = TextBlocks[5] as TextBlock;

             /* The SetBinding enables tie the dependency property
             * to the Binding object*/
            txtFirstName.SetBinding(TextBlock.TextProperty, FirstNameBind);
            txtLastName.SetBinding(TextBlock.TextProperty, LastNameBind);
            txtPseudoName.SetBinding(TextBlock.TextProperty, PseudoNameBind);
           
       
}

Afterward, let's test the Person object whether it notifies the targeted XAML UI or not. To do that, we add two event handlers to the grid object which is the TextBlock collection container.

<Grid x:Name="LayoutRoot"  MouseEnter="LayoutRoot_MouseEnter"    
                           MouseLeave="LayoutRoot_MouseLeave"  

                           Background="Azure">

The first happens when the mouse enters in the grid zone and the second happens when the mouse leaves the given grid zone. The implementation in C# is as follow:

//This happens when the mouse enters in the grid zone
        private void LayoutRoot_MouseEnter(object sender, MouseEventArgs e)
        {
            oPerson.PseudoName = "Means the best one";
        }
        //This happens when the mouse leaves in the grid zone
        private void LayoutRoot_MouseLeave(object sender, MouseEventArgs e)
       
{
            oPerson.PseudoName = "Yougerthen";
        }

Now, let's test the application, when the mouse enters the grid zone is presented as under:



Figure 1

And when the mouse leaves the grid zone is presented as follow



Figure 2

You tell me OK, I will comment the Person class updates as follow

public class Person/*:INotifyPropertyChanged*/
    {
        public Person() { }
        public Person(string FirstName, string LastName, string PseudoName)
        {
            this.FirstName = FirstName;
            this.LastName = LastName;
            this.PseudoName = PseudoName;

        }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        private string _PseudoName;
        public string PseudoName
        {
            get { return _PseudoName; }
            set
            {
                _PseudoName = value;
                //if (PropertyChanged != null)
                //{
                //    PropertyChanged(this, new PropertyChangedEventArgs("PseudoName"));
               //}

             }
       }

         //#region INotifyPropertyChanged Members

        //public event PropertyChangedEventHandler PropertyChanged;

        //#endregion
    }

And it will work since the event handlers are implemented to do the job. Then I will tell you douyyoo, nothing will happen as if the events aren't implemented at all. In fact, they are OK, I mean the event implementations, but the notification mechanism is not.

That's it
 
Good Dotneting!!!
 

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor