"Programming WPF" Translation 4th Chapter 2. Data binding

Source: Internet
Author: User

We manually write code to keep the UI and data synchronized. Effectively binds two sets of attributes together implicitly, one from the person object and the other from the control that displays the person object. Data binding is used to explicitly bind properties from one object to another, keep them synchronized, and convert to the appropriate type, as shown in Figure 4-7.

Figure 4-7

4.2.1 Binding

Instead of manually setting the Text property of the TextBox object in code and guaranteeing that they are up to date, data binding allows us to use an instance of the binding object to set the Text property, as shown in example 4-8.

Example 4-8

<TextBox >
  <TextBox.Text>
    <Binding Path="Age" />
  </TextBox.Text>
</TextBox>

In example 4-8, we have used the property element syntax described in the first chapter to create an instance of the binding class, initialize its Path property to "age", and set the binding object to the Text property value of the TextBox object. Using the binding label (also introduced in the first chapter), we can sample 4-8 abbreviated to Example 4-9.

Example 4-9

<TextBox TextContent="{Binding Path=Age}" />

As a shorter abridged version, you can omit the specified path together, and binding can also see what that means, as in example 4-10.

Example 4-10

<TextBox TextContent="{Binding Age}" />

I prefer to display syntax declarations, so I don't use the syntax of example 4-10, but I don't comment--if you like.

The binding class provides a variety of interesting tools for managing bindings between attributes, but we are more concerned with the path attribute. Most occasions, you might think of path as the property name of an object, as a data source. Therefore, the binding statement of example 4-10 establishes the binding between the Text property of a TextBox and the Name property of an object, as shown in Figure 4-8.

Figure 4-8

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.