Data binding for WPF learning

Source: Internet
Author: User

Data binding in WPF provides a powerful feature. Compared with the ordinary WinForm program, its binding function provides us with a lot of convenience, such as binding object automatic notification/refresh, converter,validation rules,two Way binding and other functions, save a lot of maintenance of the tedious work. In addition to the data template features provided in WPF, we can easily customize the modules that can be reused for control rendering-but this is done with ease and ease of use as a result of data binding. Data providers such as XmlDataProvider and ObjectDataProvider simplify the process of binding and rendering objects in a specific way. It can be said that data binding is one of the features in WPF that lets us really start to embody its convenience, but it's important for data-driven applications.

The key to data binding is the System.Windows.Data.Binding object, which binds two objects (between UI objects and UI objects, between UI objects and. NET Data Objects) in a specified manner, and establishes a communication channel between them, once the binding is established, In the next application lifecycle, it can do all the synchronization work on its own. Depending on their application, we will discuss each of the following sections in this article:

· The binding between objects

· Binding to a collection

· Data templates

· Adding rules and converters to bindings

Binding between 1.UI objects

The binding between UI objects, and also the most basic form, is usually the binding (copying) of a property value of source object origin to a property on the target object destination. The Source property can be any type, but the target attribute must be a dependency property (Dependency). In general, the binding source and target properties between UI objects are dependent (some properties are not), because the dependency property has a vertical inline change notification mechanism, and WPF can keep the target and source properties synchronized.

A simple example is how to implement data Binding in XAML:

<window x:class= "Allan.WpfBinding.Demo.Window1"
Xmlns= ""
Xmlns:x= ""
title= "Basic bindings" height= "width=" style= "{StaticResource WindowStyle}" >
<Grid>
<Grid.RowDefinitions>
<rowdefinition height= "/>"
<rowdefinition height= "*"/>
<rowdefinition height= "/>"
</Grid.RowDefinitions>
<stackpanel orientation= "Horizontal" margin= "5" horizontalalignment= "right" >
<button x:name= "btnbasicbinding" content= "Basic" style= "{StaticResource ButtonStyle}" ></Button>
<button x:name= "btncollectionbinding" content= "Collection" style= "{StaticResource ButtonStyle}" ></Button >
<button x:name= "Btndatatemplate" content= "Data Template" style= "{StaticResource ButtonStyle}" ></Button>
<button x:name= "btnadvancebindings" content= "Advance" style= "{StaticResource ButtonStyle}" ></Button>
<button x:name= "Btnexit" content= "Exit" style= "{StaticResource ButtonStyle}" ></Button>
</StackPanel>
<stackpanel grid.row= "1" horizontalalignment= "left" >
<textbox x:name= "Txtname" margin= "5" width= "borderthickness=" 0 "height=" "text=" Source Element "></ Textbox>
<textblock x:name= "Tbshowmessage" margin= "5" width= "height=" "text=" {Binding elementname=txtname,path=text } "/>
</StackPanel>
</Grid>
</Window>

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.