Data Binding in Windows Presentation Foundation (WPF) (binding controls to control values)

Source: Internet
Author: User

--------------------------------------------------------------------------------
Retain the following information when referencing or reprinting:
Da Keshan[MSN: a3news (AT) hotmail.com]
Http://www.zpxp.com http://www.brawdraw.com
Radish RAT online Image Processing
--------------------------------------------------------------------------------

WPF supports the following types of bindings:
(1) binding controls to controls
(2) binding XML data (including inline XML data and external XML file data)
(3) Data Binding of. Net objects
(4) Data Binding such as dataset and datatable
The following articles will explain the Data Binding situations one after another, hoping to help you learn about WPF.

1. binding controls to controls
<Window xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Canvas X: Name = "layoutroot">
<Textbox X: Name ="Txtboxinput"Text= "Enter the text here, and the text below will change! "Width =" 404 "/>
<Textblock X: Name ="Txtblockoutput"Width =" 404 "canvas. Top =" 23.687 ">
<Textblock. Text>
<Binding elementname ="Txtboxinput"Path ="Text"/>
</Textblock. Text>
</Textblock>
</Canvas>
</WINDOW>

Note:
(1) The text (Text attribute) entered by txtboxinput is bound to the text attribute of txtblockoutput below. When the text in the textbox changes, the text displayed in the textblock below also changes, enabling data binding.
(2) The binding method is to use <binding elementname ="Txtboxinput"Path ="Text"/>. Here, the value of the elementname attribute is the name of the source control, and the path attribute value is the text attribute of the source control.

To make the syntax of XAML more concise and clear, you can use "{Binding elementname= Source object name,Path= Source object property value name:
For example:
<Window
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
>
<Canvas X: Name = "layoutroot">
<Textbox X: Name ="Txtboxinput"Text= "Enter the text here, and the text below will change! "Width =" 404 "/>
<Textblock X: Name ="Txtblockoutput"Text ="{Binding elementname = txtboxinput, Path = text}"Width =" 404 "canvas. Top =" 23.687 "/>
</Canvas>
</WINDOW>
We found that,TxtblockoutputThe text attribute of is enclosed by "{}", which contains statements such as binding elementname = xxx and Path = xxx. Note that the attribute values are separated by commas. This is the syntax of XAML.

Take a look at the final part:

Next, use xmldataprovider to bind data.

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.