Thông tin tài liệu:
Essential Silverlight 3- P6: Khái quát Silverlight 3 không chỉ là lấp đầy với các chi tiết kỹ thuật, ví dụ rõ ràng,
và thực hiện lời khuyên rằng sẽ làm cho bất kỳ ứng dụng Silverlight tốt hơn, nhưng
Ashraf cũng bao gồm những hiểu biết rằng chỉ có thể đến từ một trong những nhà phát triển dẫn
của thời gian chạy Silverlight. Từ đồ họa, văn bản, để phương tiện truyền thông cuốn sách này-
có tất cả các thông tin cần thiết về thời gian chạy lõi 3 Silverlight....
Nội dung trích xuất từ tài liệu:
Essential Silverlight 3- P6
218 Chapter 10: Data Binding
this.ManagerTextBlock.SetBinding(
TextBlock.TextProperty,
new Binding(Manager)
);
}
DataContext Inheritance
In the previous example, the MainPage constructor set the DataContext
property, whereas its child TextBlock elements specified the binding. This
usage pattern works because the DataContext property is an inherited
property, that is, Silverlight determines the value of the property by finding
the nearest parent element with the property explicitly set.
You can reset a binding object connection by either calling the
ClearValue method for the property or by setting the property to some
other explicit value.
Technical Insight
Silverlight only provides built-in markup extensions; you cannot define your
own. Future Silverlight versions will likely let you write your own markup
extensions. The most general form of a markup extension is syntax for cre-
ating an object and providing that object with a FrameworkElement
instance and the DependencyProperty to set.
Technical Insight
Silverlight freezes a binding object when you set the binding to a property,
and you will no longer be able to modify the binding properties.
DEBUGGING TIP
Errors in binding connections do not throw exceptions by default. To
determine why a connection has failed, you can view the error mes-
sages in your debugger output window. Later in this chapter, you will
learn how to enable data-binding exceptions.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Data Binding Obje cts 219
Data Synchronization and Binding Modes
After establishing a binding between a data object and an element property,
you may need to synchronize data values when the data object properties
change or if the element property changes. For example, if the data that is
bound to a control changes, the binding needs to notify the control to
update its displayed value. If a control value changes, the binding may
need to write the data back to a data store.
To use data binding to synchronize your data with an element property,
first ensure that your data object implements INotifyPropertyChanged:
public class MyDataItem : INotifyPropertyChanged
{
//
// Set a default value in the constructor
//
public MyDataItem()
{
this.employee = ;
}
//
// INotifyPropertyChanged implementation
//
public event PropertyChangedEventHandler PropertyChanged;
//
// Employee property
//
public string Employee
{
get
{
return this.employee;
}
set
{
this.employee = value;
// Call the PropertyChanged handler
if (PropertyChanged != null)
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
220 Chapter 10: Data Binding
{
PropertyChanged(this, new PropertyChangedEventA rgs(Employee));
}
}
}
private String employee;
}
To control how properties synchronize, you can set the binding mode
of a Binding object to OneTime, OneWay, or TwoWay. OneTime indicates that
Silverlight will read the data only once and will never update values when
properties are changed. OneWay indicates that changes to a data object will
change the element property, but changes to the element property will not
change the data object. With a OneWay binding, changes to the element
properties will disconnect the Binding object and will no longer synchro-
nize data. TwoWay indicates that Silverlight will synchronize changes to the
element property with the data object and changes to the data object with
the element. The default binding mode is OneWay.
You can specify the binding mode by setting the Mode property on a
Binding object, or declaratively through the markup extension:
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Data Binding Obje cts 221
Changes to MyDataItem or TextBlock properties then synchronize based
on the binding mode:
MyDataItem dataItem = new MyDataItem();
this.DataContext = dataItem;
// Updates only the TextBlocks set to bind mode
// OneWay and TwoWay. Does not update the OneTime
// binding mode TextBlock.
dataItem.Employee = Mark B;
// Does not update the data source ...