[Silverlight] binding property data to custom types

Source: Internet
Author: User

In the current. netProgramData Binding is often used, because data binding can greatly simplify a common process-Data Source changes, reflecting the data view of the data source for corresponding updates. When talking about this process, it is easy to think of the observer pattern in the design pattern. Obviously, the design of data binding is an implementation of this pattern. This document uses the following example to describe how to bind Custom Data Types in Silverlight or WPF.

Now there is a form interface that displays detailed information about the customer, including name, age, and gender.

The following are the custom types of customerCode:

Code

Public Class Customer: inotifypropertychanged
{
Private String M_name;
Public String Customername
{
Get { Return M_name ;}
Set
{
If (M_name = Value)
Return ;

M_name=Value;

If(Propertychanged! = Null)
Propertychanged (This,NewPropertychangedeventargs ("Customername"));
}
}

Private ByteM_age;
Public ByteAge
{
Get{ReturnM_age ;}
Set
{
If(M_age=Value)
Return;

M_age=Value;

If (propertychanged ! = null )
propertychanged ( This , New propertychangedeventargs ( " age " );
}< BR >}

Private BoolM_ismale;
Public BoolIsmale
{
Get{ReturnM_ismale ;}
Set
{
If(M_ismale=Value)
Return;

M_ismale=Value;

If(Propertychanged! = Null)
Propertychanged (This,NewPropertychangedeventargs ("Ismale"));
}
}

# RegionInotifypropertychanged members

Public EventPropertychangedeventhandler propertychanged;

# Endregion
}

The preceding definition of the customer class inherits the inotifypropertychanged interface, which is defined in the namespace of system. componentmodel. This interface has only one member, which is the propertychanged event. From the bold code, we can see that the propertychanged event is triggered in the Set accessors of the three attributes of customer, and each event parameter is named as the parameter corresponding to the assigned attribute. (Note that the verification code of the Set accessors does not trigger this event if the current value is not modified)

By triggering this event, data binding to the customer instance can be performed smoothly.

The Silverlight control code of the form is as follows:

XAML:

Code

< Usercontrol X: Class = "Silverlightapplication5.mainpage"
Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "Http://schemas.microsoft.com/winfx/2006/xaml"

Loaded = "Usercontrol_loaded" >
< Stackpanel X: Name = "Stp_customerinfo" >
< Textblock X: Name = "Tb_name" Text =" {Binding customername} " Fontsize = "18" />
< Textblock X: Name = "Tb_age" Text =" {Binding age} " Fontsize = "17" />
< Textblock X: Name = "Tb_sex" Text =" {Binding ismale} " Fontsize = "16" />

<ButtonX: Name= "Btn_changename"Content= "Change name to foo"Click= "Btn_changename_click"Horizontalalignment= "Left"/>
</Stackpanel>
</Usercontrol>

Backend C # code:

Code

Public Partial Class Mainpage: usercontrol
{
Public Mainpage ()
{

Initializecomponent ();

}

Private Void Usercontrol_loaded ( Object Sender, routedeventargs E)
{
Stp_customerinfo.datacontext = New Customer () {customername = " Agile " , Age = 18 , Ismale = True };
}

Private VoidBtn_changename_click (ObjectSender, routedeventargs E)
{
Customer C=Stp_customerinfo.datacontextAsCustomer;

C. customername= "Foo";
}

}

The above code is a very simple client information form control. In the loaded event of the control, a hard-coded data source is set for the container stp_customerinfo, that is, a customer instance. The three textblock controls in the container inherit the data source (of course, you can also set the same customer as the data source one by one ). Running the Silverlight program has the following results:

Then, when you click the button, you can see from the event processing code of the button that the event handler will get the customer instance that has been set, and then modify its customername attribute to "foo ". Effect after clicking the button:

This is the process of binding property data of the custom type. The method is simple, but it is of great value.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.