wpf/silverlight2.0 Binding (data binding) mechanism detailed

Source: Internet
Author: User

What is Data binding

The DataContext property in the control base class (FrameworkElement, FrameworkContentElement) in WPF technology implements the binding mechanism, which is also supported in XAML. When the DataContext of a control changes, the DataContext of its child controls also inherits the DataContext of the parent control (provided that the child control is not assigned another value). The properties of the child control get the data from the data source, which enables XAML to get the values in the data source through the binding tag. Refresh data updates in each of its child controls when the data source is updated to achieve Toruk macto called effects!

Binding binding Tag

The XAML processor supports the syntax of the binding mechanism, appearing in the {Binding propertyname=value} format. Where PropertyName is basically divided into "data source specified tags, read data source tags, additional option tags," value can use other tags to get special values. A binding expression can be used in combination with a data source tag, a read data source tag, an additional option tag, or a parent control that inherits the data source of the parent control and binds it.

Grammar

<TextBox Name="theTextBox" />
     <TextBlock Text="{Binding ElementName=theTextBox, Path=Text}"/>
     同于
     <TextBox Name="theTextBox" />
     <TextBlock>
                         <TextBlock.Text>
                               <Binding ElementName="theTextBox" Path="Text" />
                         </TextBlock.Text>
     </TextBlock>

Data source specified tag

Source property: To make any type of object instance as a data source through other extension tags, you can use tag settings such as StaticResource. For example: "{Binding source={staticresource xkeyelement}, Xpath=//item}", Xkeyelement is an object of XmlDataProvider. Gets the data as a data source with all the item-labeled nodes in Xkeyelement.

ElementName property: Make any object in the current XAML document with a value of elementname (x:name) as the data source. For example: "{Binding elementname=listbox1, path=selecteditem}" means that the SelectedItem property of ListBox1 is the data source of the current control.

RelativeSource property: Relative to the data source, use RelativeSource to make the object associated with itself a data source. For example: "Relativesource={relativesource Self}", the reader notes that "the middle is relativesource{} is an extension tag, and the outside relativesource={} is the binding tag property". This instance code means to get itself as a data source.

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.