Three types of bindings are supported in Silverlight: oneway, twoway, and onetime. The default value is oneway.
One-way indicates that only the data source is bound to the target (usually the UI object), one-way;
Twoway indicates that the data source can be bound to the target, and changes to the target can also be reported to the data source for updates.
Onetime is a special case of oneway. It only loads data once. Subsequent data changes do not notify the bound target object. In this way, it can bring better performance.
The binding syntax can be represented by braces. The following are several examples:
< Textblocktext = "{Binding age }" / >
Equivalent:
< Textblock text = "{Binding Path = age }" / >
Or explicitly write the binding direction:
< Textblock text = "{Binding Path = age, mode = oneway }" / >
According to the data binding semantics, the default value is oneway. That is to say, if the background data changes, the control related to the binding relationship established in the foreground should also be updated.
For exampleArticle(1) Change the data source mentioned in to a private member on the current page, and then change the value in a button click event.CodeAs follows:
Public Partial Class Page: usercontrol
{
Private List <person> persons;
Public Page ()
{
Initializecomponent ();
Persons =NewList <person> ();
For(VAR I = 0; I <5; I ++)
{
Persons. Add (NewPerson {name ="Person"+ I. tostring (), age = 20 + I });
}
List1.datacontext = Persons;
}
Private VoidButton_click (ObjectSender, routedeventargs E)
{
Persons [0]. Name ="Tom";
}
}
< ListBox X: Name = "List1" >
< ListBox. itemtemplate >
< Datatemplate >
< Stackpanel orientation = "Horizontal" >
< Textblock text = "{Binding age }" Margin = "20, 0" / >
< Textblock text = "{Binding name }" / >
< /Stackpanel >
< /Datatemplate >
< /ListBox. itemtemplate >
< /ListBox >
However, when we click the button, we find that the data in the ListBox has not changed. This is because no notification is sent when the data source is updated.
We can enable the inotifypropertychanged interface for objects in the data source to send related notifications when the bound source attribute value changes.
The Code is as follows:
Public Class Person: inotifypropertychanged
{
Private Int Age;
Public Int Age
{
Get
{
Return Age;
}
Set
{
Age = Value ;
Notifypropertychange ( "Age" );
}
}
Private String Name;
Public String Name
{
Get
{
Return Name;
}
Set
{
Name = Value ;
Notifypropertychange ( "Name" );
}
}
Public Event Propertychangedeventhandler propertychanged;
Private VoidNotifypropertychange (StringPropertyname)
{
If(Propertychanged! =Null)
{
Propertychanged (This,NewPropertychangedeventargs (propertyname ));
}
}
}
The principle of this code is very simple. I will not explain it here. Then, click the button to change the name of the first data entry in The ListBox to Tom:
Http://www.cnblogs.com/RChen/archive/2008/07/03/1235039.html
This article from the cpcpc blog, original address: http://blog.csdn.net/cpcpc/article/details/6900418