WPF and Silverlight Learning notes (20): WPF Data Binding Overview

Source: Internet
Author: User
Tags bind silverlight

WPF data binding provides an application with a simple and consistent way to represent data and interact with the data. Elements can be bound to data in various data sources in the form of common language runtime (CLR) objects and XML.

First, the basic concept of data binding:

Data binding involves two aspects: one is the source of the binding, and the other is the binding target. The binding source is the source data used by the control bindings, which is the control that the data displays.

1, for the binding source, in WPF can be the following four kinds:

CLR objects: A exposed property, child property, indexer that can be bound to a CLR class

Ado. NET objects: such as DataTable, DataView, etc.

XML files: Parsing with XPath

DependencyObject: Binding to its dependency property, that is, the control-bound control

2. For binding targets, you must be a dependencyobject in WPF to bind data to its dependency properties.

Second, the basic way of binding

Depending on the direction of the data flow, data binding in WPF is divided into the following four categories:

OneWay binding causes changes to the Source property to automatically update the target property, but changes to the target property do not propagate back to the source property. This binding type applies to situations where the bound control is an implicitly read-only control. For example, you might bind to a source such as a stock ticker, or the target property has no control interface for making changes, such as a table's data-bound background color. If you do not need to monitor changes to the target property, using the oneway binding pattern avoids the overhead of twoway binding mode.

TwoWay binding causes changes to the Source property to automatically update the target property, and changes to the target property automatically update the Source property. This binding type applies to editable forms or other fully interactive UI scenarios. Most properties default to OneWay bindings, but some dependency properties (typically the properties of a user-editable control, such as the Text property of a TextBox and the IsChecked property of a CheckBox) default to TwoWay binding. The programmatic way to determine whether a dependency property binding is one-way or bidirectional by default is to use GetMetaData to get property metadata for the property, and then to check the Boolean value of the Bindstwowaybydefault property.

OneWayToSource is the opposite of the oneway binding; It updates the Source property when the target property changes. An example scenario is where you only need to recalculate source values from the UI.

Onetime binding, which causes the Source property to initialize the target property, but does not propagate subsequent changes. This means that if the data context has changed, or if the object in the data context has changed, the change is reflected in the target attribute. This binding type is appropriate if you are using a snapshot of the current state of the data that is appropriate for use, or if the data is truly static. You can also use this binding type if you want to use a value from the Source property to initialize the target property, and you do not know the data context in advance. This binding type is essentially a simplified form of a oneway binding that provides better performance if the source value does not change.

The default values for each dependency property are different. In general, users can edit control properties, such as properties of text boxes and check boxes, by default to two-way binding, while most other properties default to one-way bindings. The programmatic way to determine whether a dependency property binding is one-way or bidirectional by default is to use GetMetaData to get property metadata for the property, and then to check the Boolean value of the Bindstwowaybydefault property.

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.