Chapter 2. Data Binding (1)

Source: Internet
Author: User

We manually write code to ensure UI and data synchronization. The two groups of attributes are implicitly bound together. One group comes from the person object and the other is from the controls that display the person object. Data Binding is used to explicitly bind attributes from one object to another to keep them synchronized and convert them to an appropriate type, as shown in Figure 4-7.

Figure 4-7

{
Get_larger (this)
} "Src =" http://img.ddvip.com/2009_03_09/1236585568_ddvip_6082.gif "alt =" programming WPF Chapter 2. Data Binding "width =" 4th "Height =" 440 ">

4.2.1 binding

To manually set the text attributes of textbox objects in the Code and ensure they are up-to-date, Data Binding allows us to set the text attributes using the binding object instance, as shown in Figure 4-8.

Example 4-8

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

In Example 4-8, we have used the attribute element syntax introduced in chapter 1 to create an instance of the binding class and initialized its path attribute as "age ", set the binding object to the text attribute value of the textbox object. Use the bound tag (also introduced in chapter 1). For example 4-8, use 4-9.

Example 4-9

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

As a shorter version, you can omit the specified path, and the binding can also know what it means, as shown in Example 4-10.

Example 4-10

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

I prefer to display the syntax Declaration, so I will not use the syntax for example 4-10, but I will not comment on it-if you like to use it

The binding class provides a variety of interesting tools to manage the binding between attributes, but we are more concerned with the path attribute. In most cases, you may consider path as the attribute name of an object and as the data source. Therefore, the binding statement in the example 4-10 creates a binding between the text attribute of a textbox and the name attribute of an object, as shown in Figure 4-8.

We manually write code to ensure UI and data synchronization. The two groups of attributes are implicitly bound together. One group comes from the person object and the other is from the controls that display the person object. Data Binding is used to explicitly bind attributes from one object to another to keep them synchronized and convert them to an appropriate type, as shown in Figure 4-7.

Figure 4-7

{
Get_larger (this)
} "Src =" http://img.ddvip.com/2009_03_09/1236585568_ddvip_6082.gif "alt =" programming WPF Chapter 2. Data Binding "width =" 4th "Height =" 440 ">

4.2.1 binding

To manually set the text attributes of textbox objects in the Code and ensure they are up-to-date, Data Binding allows us to set the text attributes using the binding object instance, as shown in Figure 4-8.

Example 4-8

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

In Example 4-8, we have used the attribute element syntax introduced in chapter 1 to create an instance of the binding class and initialized its path attribute as "age ", set the binding object to the text attribute value of the textbox object. Use the bound tag (also introduced in chapter 1). For example 4-8, use 4-9.

Example 4-9

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

As a shorter version, you can omit the specified path, and the binding can also know what it means, as shown in Example 4-10.

Example 4-10

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

I prefer to display the syntax Declaration, so I will not use the syntax for example 4-10, but I will not comment on it-if you like to use it

The binding class provides a variety of interesting tools to manage the binding between attributes, but we are more concerned with the path attribute. In most cases, you may consider path as the attribute name of an object and as the data source. Therefore, the binding statement in the example 4-10 creates a binding between the text attribute of a textbox and the name attribute of an object, as shown in Figure 4-8.

The procedure is as follows:

L The binding mechanism searches for non-empty datacontext attributes in textbox itself

L The binding mechanism searches for non-empty datacontext attributes in the grid.

L The binding mechanism searches for non-empty datacontext attributes in the window

Provide a non-empty datacontext for both text box controls. In the window1 constructor, set the shared person object as the datacontext attribute of the grid, as shown in Example 4-11.

Example 4-11

// Window1.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;

namespace PersonBinding {
  public partial class Window1 : Window {
    Person person = new Person("Tom", 9);

    public Window1(  ) {
      InitializeComponent(  );

      // Let the grid know its data context
      grid.DataContext = person;

      this.birthdayButton.Click += birthdayButton_Click;
    }

    void birthdayButton_Click(object sender, RoutedEventArgs e) {
      // Data binding keeps person and the text boxes synchronized
      ++person.Age;
      MessageBox.Show(
        string.Format(
          "Happy Birthday, {0}, age {1}!",
          person.Name,
          person.Age),
        "Birthday");
    }
  }
}

 

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.