代码改变世界

Binding

2012-01-03 18:26  一一九九  阅读(252)  评论(0编辑  收藏  举报

The binding code is analogous to the binding markup extension. it’s an entity in and of itself that represents the relationship between the source and the target of the data binding. The constructor for Binding accepts a path to the target. the path tells the binding how to get the target data, starting from the data context of source.

BindingOperation is  a utility class used for manipulation bindings. We use the SetBinding mthod to establish the relationship between the tab and the presenter. SetBinding takes three parameters. The first is the target of the data binding. The second is the dependecncy property on the target that we are binding to. the third parameter is the binding we just creatd.

in summary, there are three steps to create a functioning data binding:

1. Create an instance of Binding.

2. Establish the relationship between the target and the binding with BindingOperations.SetBing();

3. set the data Context on the target to the soure of the data .

Data templates

data templates are one of the two types of templates used in WPF. they provide a way to descrbe how data should be visualized in terms of UI Elements. This is different from deciding how  to render a datetime value, or formatting a telephone number.

data templates are a composition of UI elements. Many controls have properties of type DataTemplate. for example , itemsControl.ItemTemplate and GridView.CellTemplate.

sometimes it can get confusing to know which feature of WPF is appropriate in a given scenario. there’s some overlap in the things that styles, data templates, and control templates are able todo. to add to the confusion, you cn embed styles in your data templates, control templates in your styles, and so on. here are a few thoughts to help you decide what to use:

Styles are the simplest of the three, so if you are able to achieve what you want using styles, that is the best choice. Keep in mind that styles allow you to set nonvsual prperties as well.

control templates define the UI elements that compose a given control. that’s a lot more complicated than merely setting some properties. you should use control templates only when you really need to.

data templates resemble control templates in that they allow you to compose UI elements. they are often used with list controls to define how items in a list should be rendered.

Converts are classed that implement IValueConverter. The interface has two methods, Convert and ConvertBack. Convert is used when data flows from the source to the target. ConvertBack is used when data flows back to the source( in a two-way binding).

Both methods take four paramteters. The first is value , and it is the actual data  to be manipulated. The second is the type of the target data . the third typ if for general use and can be used to parameterize you converter. the fourth is the cultural information about the executing context.