WPF getting started tutorial series 15th -- Data Binding in WPF (1), wpf getting started tutorial

Source: Internet
Author: User

WPF getting started tutorial series 15th -- Data Binding in WPF (1), wpf getting started tutorial

Using Windows Presentation Foundation (WPF), you can easily design a powerful user interface. At the same time, WPF provides the data binding function. The data of WPF is bound to Winform and ASP. the data binding function in. NET is similar, but also different. In WPF, bind data through background code binding, foreground XAML binding, or a combination of the two. You can bind controls, public properties, XML, or objects. Data Binding in WPF is faster, more flexible, and simpler than WinForm and ASP. NET.

1. What is data binding?

Data Binding in WPF must have a binding destination and a data source to be bound. The binding target can be any accessible property or control that inherits the DependencyProperty, such as the Text property of the TextBox Control. Data sources can be attributes of other controls, including object instances, XAML elements, ADO. NET Dataset, and XML data. Microsoft provides two auxiliary classes XmlDataProvider and ObjectDataProvider for XML binding and object binding.

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

Binding is used to bind the properties of the interface control to the background data. In this way, the foreground interface is associated with the background data to achieve the interface and data coupling.

The WPF Binding engine obtains information about the following content from the Binding object:

Source object and target object.

The direction of the data stream. You can specify this direction by setting the Binding. Mode attribute.

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

The following differences exist in the binding methods of WPF, ASP. NET, and WinForm:

(1) Binding can be used to implement interface-data coupling through XAML statements. If Binding is compared to a data bridge, the two ends of Binding are the source and target respectively. Where data comes from is the source, Binding is the bridge in the middle, and Binding is the destination where data is going. Generally, the Binding source is the object of the logic layer, and the Binding target is the control object of the UI Layer. In this way, data is continuously delivered to the UI Layer through the Binding and displayed by the UI Layer, this completes the data-driven UI process. For example.


(2) Binding has an important attribute Mode to realize data flow in Binding. There are several specific types.


Member name



Use the default Mode Value of the bound target. The default values of each dependency attribute are different. In general, you can edit the control attributes (such as the attributes of the text box and check box). By default, two-way binding is used, and most other attributes are one-way binding by default. Determine whether the Dependency Property is bound to one-way or two-way by default. The method is to use GetMetadata to obtain the property metadata of the property, and then check the Boolean value of the BindsTwoWayByDefault property.


Update the binding target when the application starts or the data context changes. This binding type applies to the following situations: Suitable for using a snapshot in the current State or data in the actual static state. If you want to initialize a target attribute with a value from the source attribute without knowing the data context, you can also use this binding type. This binding type is essentially a simplified form of OneWay binding, providing better performance without changing the source value.


When the binding source (source) changes, the binding target (target) attribute is updated. This binding type applies when the bound control is an implicit read-only control. For example, it can be bound to sources such as stock market codes. Alternatively, the target property may not have a control interface for changing (for example, the table's data binding background color. If you do not need to monitor changes to the target attributes, use the OneWay binding mode to avoid system overhead in the TwoWay binding mode.


Update the source attribute when the target attribute is changed.


As a result, changes to the source or target attributes can be automatically updated to the other party. This binding type applies to editable forms or other fully Interactive UI schemes.


(3) You can configure a trigger to determine when to modify the value of the data source from the user input on the interface. It can be implemented through the UpdateSourceTrigger attribute, which has the following values:


Member name



Bind the default UpdateSourceTrigger value of the Target attribute. The default values of most dependency properties are PropertyChanged, and the default value of Text properties is LostFocus.

The Programming Method for Determining the default UpdateSourceTrigger value of the dependency attribute is to use GetMetadata to obtain the attribute metadata of the attribute, and then check the value of the defadateupdatesourcetrigger attribute.


Update the binding source only when the UpdateSource method is called.


Update the binding source when the target element has no focus.


Update the binding source immediately when the binding target property is changed.




The usage is as follows:

<TextBox Name="itemNameTextBox"

         Text="{Binding Path=ItemName, UpdateSourceTrigger=Explicit}" />





2. Simple binding

Next is the first example in this article. A very simple binding example shows how to bind the values in the ListBox to TextBlock.

First, seven listboxitems are added to ListBox as The ListBox option.

Next, bind the Text of the second TextBlock to the selected ListBox item through Binding. The ElementName attribute in the Binding syntax indicates the name of the control to be bound to the Text attribute of TextBlock. The Path attribute indicates that the attribute of The ListBox element will be bound to the Text attribute. The Code is as follows.

<Window x:Class="WpfApp1.WindowBindData"
Title="WindowBindData" Height="400" Width="500">
<RowDefinition Height="150"/>
<RowDefinition Height="150"/>
<RowDefinition Height="138*"/>
<StackPanel Grid.Row="0">
< TextBlock width = "248" height = "24" text = "Stock Name:"
<ListBox x:Name="listStockName" Width="248" Height="56">
< ListBoxItem content = "general education" / >
< ListBoxItem content = "great wisdom" / >
< ListBoxItem content = "Baosteel Co." / >
< ListBoxItem content = "Shanghai Pudong Development Bank" / >
< ListBoxItem content = "ICBC" / >
< ListBoxItem content = "CSCEC" / >
< ListBoxItem content = "CSR" / >
< TextBlock width = "248" height = "24" text = "the stock name you selected:" / >
<TextBlock Width="248" Height="24" Text="{Binding ElementName=listStockName, Path=SelectedItem.Content}">

The result is as follows.


Related Article

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.