Introduction to Windows 8 Style application development 25 data binding

Source: Internet
Author: User
Tags bind

Data binding is a simple way to display data, and the connection or binding between a UI element and a data object allows the data to flow between the two. When a binding is established and the data changes, the corresponding UI element automatically displays the change.

How to bind a UI element to data

As you can see from the diagram above, each binding must specify a source and a target.

Where the source object can be any CLR object, including the target element itself and other UI elements. The target can be any DependencyProperty (dependent attribute) of the FrameworkElement.

The data-binding engine obtains the following from the binding object:

1) Source object and target object;

2 The direction of data flow;

(3) value converter;

For example, use C # code and XAML to bind the foreground property of a TextBox.

XAML Code:

<textboxx:name= "MyTextBox" text= "Text" foreground= "{Binding Brush1, 

mode=oneway}"/>

C # code:

MyColors textcolor = new mycolors

();
TextColor. BRUSH1 = new SolidColorBrush

(colors.red);
Mytextbox.datacontext = TextColor;

Bindings are using {Binding ...} Syntax is created in XAML. The source is set in code by setting the DataContent property of the TextBox.

In addition, the data will be inherited. If we set the data context on a parent element, its child elements will use the same data. We can bind to a property of the source object by setting the Binding.path property.

Direction of data binding

Each binding contains a mode property that determines how and when the data flows.

Three kinds of bindings:

1) Onetime: bindings use source data to update targets at creation time.

2) OneWay: Binding will use source data to update the target (default mode) when it is created and when the data changes.

3) TwoWay: Binding updates both the target and the source when any one of the target and source changes occurs.

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.