WPF Getting Started Tutorial series 15 data Binding in--WPF (i)

Source: Internet
Author: User

Using Windows Presentation Foundation (WPF) makes it easy to design a powerful user interface, while WPF provides data binding capabilities. WPF's data binding is similar to WinForm with data binding in ASP. NET, but also differs in that it is data binding in WPF in the form of background code binding, binding in the foreground XAML, or a combination of both. You can bind controls, public properties, XML, or objects, and data binding in WPF is faster, more flexible, and simpler than WinForm and ASP.

First, what is data binding

Data binding in WPF, you must have a binding target and a data source to bind to. The binding target can be any accessible property or control that inherits from DependencyProperty, such as the TextBox control's Text property. A data source can be a property of another control, which can be an object instance, a XAML element, an ADO dataset, and XML data. Microsoft provides two helper classes XmlDataProvider and ObjectDataProvider for XML binding and object binding.

What is the difference between WPF's data binding and the data binding in ASP. WinForm? The biggest difference is that WPF uses {Binding ...} This statement.

Binding is used to implement the interface between the properties of the control and the background data, through this form of the foreground interface with the background data to achieve interface and data coupling purposes.

The WPF binding engine obtains information about the following from the binding object:

The source object and the target object.

The direction of the data flow. You can specify this direction by setting the Binding.mode property.

Value converter (if present). You can specify a value converter by setting the Converter property to an instance of the class used to implement IValueConverter.

There are several differences between WPF and the way in which ASP. NET and WinForm are bound:

(1) binding can be used to implement interface and data coupling through the XAML statement. If the binding is a bridge to data, then its ends are the source and target of the binding. Where the data comes from is the source, the binding is the bridge in the middle, and the binding target is where the data goes. In general, the binding source is the logical layer of the object, the binding target is the UI layer of the control object, so that the data will flow through the binding to the UI layer, is presented by the UI layer, also completed the data-driven UI process. Such as.

(2) The binding has an important attribute mode, which implements the data flow in the binding. There are several specific.

Member name

Description

Default

Use the default Mode value of the binding target. The default values for each dependency property are different. In general, user-editable control properties, such as the properties of text boxes and check boxes, 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 unidirectional or bidirectional by default is to get the property metadata for the property by using GetMetaData, and then examine the Boolean value of the Bindstwowaybydefault property.

OneTime

Updates the binding target when the application starts or when the data context changes. This binding type is suitable for situations where a snapshot using the current state is suitable for use or data that is actually static data state. You can also use this binding type if you want to initialize a target property with a value from the Source property, and you do not know the data context beforehand. This binding type is essentially a simplified form of OneWay binding that provides better performance in the case where the source value does not change.

OneWay

When the binding source (source) changes, the binding target (target) property is updated. This binding type applies to cases in which the bound control is an implicitly read-only control. For example, you can bind to a source such as a stock ticker. Alternatively, the target property may not have a control interface for making changes, such as a data-bound background color for a table. If you do not need to monitor changes to the target properties, use OneWay binding mode to avoid the overhead of TwoWay binding mode.

OneWayToSource

Updates the Source property when the target property changes.

TwoWay

Causes changes to the source or target properties to automatically update each other. This binding type applies to editable forms or other fully interactive UI scenarios.

(3) You can configure the trigger to determine when the user input data in the interface to modify the value of the data source. Can be implemented by the UpdateSourceTrigger property, with the following values

Member name

Description

Default

The default UpdateSourceTrigger value of the binding target property. The default value for most dependency properties is propertychanged, and the default value for the Text property is LostFocus.

The programmatic way to determine the default UpdateSourceTrigger value for a dependency property is to use GetMetaData to get the property metadata for the property, and then examine the value of the Defaultupdatesourcetrigger property.

Explicit

The binding source is updated only when the Updatesource method is called.

LostFocus

Updates the binding source when the binding target element loses focus.

PropertyChanged

When the binding target property changes, the binding source is updated immediately.

The specific usage is as follows:

<textbox name= "Itemnametextbox"         

Second, a simple binding

Next is the first example of this article, a very simple binding example that shows how to bind a selected value in a ListBox to TextBlock.

First, add seven ListBoxItem to the listbox as a listbox option.

Second, the text of the second TextBlock is bound to the ListBox selection by binding. The ElementName property in the binding syntax indicates the name of the control to which the TextBlock's Text property is bound. The Path property indicates that we will bind to the property of the ListBox element on the Text property. The specific code is as follows.

<window x:class= "Wpfapp1.windowbinddata" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml" title= "Windowbinddata" height= "$" width= "> &lt" ; grid> <Grid.RowDefinitions> <rowdefinition height= "/> <rowdefinition He"  ight= "/> <rowdefinition height=" 138* "/> </Grid.RowDefinitions> <stackpanel            grid.row= "0" > <textblock width= "248" height= "text=" Stock Name: "textwrapping=" Wrap "/>                <listbox x:name= "Liststockname" width= "248" height= "> <listboxitem content=" All-through education "/> <listboxitem content= "Great wisdom"/> <listboxitem content= "Baosteel stock"/> <listboxit Em content= "pu FA Bank"/> <listboxitem content= "ICBC"/> <listboxitem content= "China Architecture"/&G                T <lIstboxitem content= "China South Car"/> </ListBox> <textblock width= "248" height= "text=" the name of the stock you selected said: "/> <textblock width=" 248 "height=" text= "{Binding elementname=liststockname, Path=selecteditem.con Tent} "> </TextBlock> </StackPanel> </Grid> </Window>

Effects such as.

WPF Getting Started Tutorial series 15 data Binding in--WPF (i)

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.