The binding--notes of WPF in layman's note (2015.03.02)

Source: Internet
Author: User

Binding

The DataContext attribute is defined in the FrameworkElement class, which is the base class for WPF, which means that all WPF controls have this property. On the UI tree, each node has datacontext. When a binding knows only the path and does not know the source, it goes all the way to the root of the tree along the UI tree, looking at the node's DataContext if it has the attribute specified by path. If so, take the object as its source, or continue to find it. If the root of the tree has not been found, then the binding has no source, so it will not get the data. Examples are as follows:

Xaml:

<stackpanel background= "LightBlue" >
<StackPanel.DataContext>
<local:student id= "6" name= "Tim" age= "/>"
</StackPanel.DataContext>
<Grid>
<StackPanel>
<textbox text= "{Binding Id}" margin= "5"/>
<textbox text= "{Binding Name}" margin= "5"/>
<textbox text= "{Binding age}" margin= "5"/>
</StackPanel>
</Grid>
</StackPanel>

C # code:

public class Student
{
public int Id {get; set;}
public string Name {get; set;}
public int Age {get; set;}
}

<summary>
The interactive logic of MainWindow.xaml
</summary>
public partial class Mainwindow:window
{
Public MainWindow ()
{
InitializeComponent ();
}
}

Because path can also be omitted as ".", it has the following code:

<stackpanel background= "LightBlue" >
<StackPanel.DataContext>
<sys:string>hello datacontext!</sys:string>
</StackPanel.DataContext>
<Grid>
<StackPanel>
<textbox text= "{Binding.}" margin= "5"/>
<textbox text= "{Binding.}" margin= "5"/>
<textbox text= "{Binding.}" margin= "5"/>
</StackPanel>
</Grid>
</StackPanel>

DataContext is a dependency property, one of the most important features of a dependency property is that when an assignment is not displayed for a dependency property of a control, the control treats its own container's property value as its own property value, which is actually the property value passed down the UI element tree. As follows:

<grid background= "LightBlue" datacontext= "6" >
<Grid>
<Grid>
<Grid>
<button x:name= "btn" content= "OK" click= "Btn_click"/>
</Grid>
</Grid>
</Grid>
</Grid>

C # code:

public partial class Mainwindow:window
{
Public MainWindow ()
{
InitializeComponent ();
}

private void Btn_click (object sender, RoutedEventArgs e)
{
MessageBox.Show (btn. Datacontext.tostring ());
}
}

The list-style controls in WPF derive from the Itemscotrol class and inherit the ItemsSource property, which receives an instance of the IEnumerable interface derived class as its own value. Each derived class of ItemsControl has its own corresponding entry container. The ItemsSource store is a piece of data, in order to display the data need to put on their "coat", the item container plays the role of the data cloak. As long as the ItemsSource property value is set for a ItemsControl object, the ItemsControl object automatically iterates over the data element, prepares an entry container for each data element, and uses the binding to establish an association between the item container and the data element. As follows:

Xaml:

<stackpanel x:name= "StackPanel" background= "LightBlue" >
<textblock text= "Student ID:" fontweight= "Bold" margin= "5"/>
<textbox x:name= "Textboxid" margin= "5"/>
<textblock text= "Student List:" fontweight= "Bold" margin= "5"/>
<listbox x:name= "listboxstudents" height= "" "margin=" 5 "/>
</StackPanel>

C # code:

    public partial class Mainwindow:window
    {
         public MainWindow ()
        {
             InitializeComponent ();

list<student> stulist = new list<student>
{
New Student {Id = 1, Name = "Tom", age = 20},
New Student {Id = 2, Name = "Tim", age = 21},
New Student {Id = 3, Name = "Bruce", age = 22}
};

This.listBoxStudents.ItemsSource = stulist;
This.listBoxStudents.DisplayMemberPath = "Name";

This.textBoxId.SetBinding (Textbox.textproperty, New Binding ("Selecteditem.id") {Source = this.listboxstudents});
}
}


This article is from the "Ink Pool small" blog, please be sure to keep this source http://306702895.blog.51cto.com/8366753/1616982

The binding--notes of WPF in layman's note (2015.03.02)

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.