WPF Study Notes-9. Binding (2)

Source: Internet
Author: User

4. bind to a collection

In actual development, we usually bind a collection Data Object (such as a data table) to a DataGrid or
In this case, we need to use the set binding method on The ListBox list control. WPF specifically implements
System. Collections. objectmodel. observablecollection <t>
The generic set saves the time for writing the code of the set with the change notification function.

Window1.xaml

<Window X: class = "learn. WPF. window1"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "window1">
<Grid>
<Stackpanel>
<ListBox X: Name = "listbox1"> </ListBox>
</Stackpanel>
</GRID>
</WINDOW>

Window1.xaml. CS

Public class personal
{
Public string name {Get; set ;}
Public int age {Get; set ;}
Public String sex {Get; set ;}
}

Public class personallist: observablecollection <personal>
{
}

Public partial class window1: Window
{
Public window1 ()
{
Initializecomponent ();

VaR list = new personallist
{
New personal {name = "Tom", age = 10, sex = "male "},
New personal {name = "Mary", age = 15, sex = "female "},
New personal {name = "Jack", age = 12, sex = "male "},
};

VaR binding = new binding {source = List };
This. listbox1.setbinding (ListBox. itemssourceproperty, binding );
This. listbox1.displaymemberpath = "name ";
}
}

Note that the displaymemberpath attribute is used to specify the content of listboxitem. Otherwise, tostring () is called to display the result.

Of course, you can also bind the list data in the logical resource directly.

Window1.xaml

<Window X: class = "learn. WPF. window1"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: My = "CLR-namespace: Learn. WPF"
Title = "window1">
<Window. Resources>
<My: personallist X: Key = "personals">
<My: Personal Name = "Tom" age = "10" Sex = "male"/>
<My: Personal Name = "Mary" age = "15" Sex = "female"/>
<My: Personal Name = "Jack" age = "12" Sex = "male"/>
</My: personallist>
</Window. Resources>
<Grid>
<Stackpanel>
<ListBox X: Name = "listbox2"
Itemssource = "{binding source = {staticresource personals }}"
Displaymemberpath = "name">
</ListBox>
</Stackpanel>
</GRID>
</WINDOW>

To use personal and personallist, we introduced a CLR namespace.

In the master-slave structure (master-detail) display, we usually need to implement the "select item tracking" function, that is, when we select a record of the master table, other details controls must synchronously refresh the details of the record.

<Window X: class = "learn. WPF. window1"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: My = "CLR-namespace: Learn. WPF"
Title = "window1">
<Window. Resources>
<My: personallist X: Key = "personals">
<My: Personal Name = "Tom" age = "10" Sex = "male"/>
<My: Personal Name = "Mary" age = "15" Sex = "female"/>
<My: Personal Name = "Jack" age = "12" Sex = "male"/>
</My: personallist>
</Window. Resources>
<Grid>
<Stackpanel>
<ListBox X: Name = "listbox1"
Itemssource = "{binding source = {staticresource personals }}"
Displaymemberpath = "name"
Issynchronizedwithcurrentitem = "true">
</ListBox>

<Label X: Name = "lblname" content = "{binding source = {staticresource personals}, Path = Name}"/>
<Label X: Name = "lblage" content = "{binding source = {staticresource personals}, Path = age}"/>
<Label X: Name = "lblsex" content = "{binding source = {staticresource personals}, Path = sex}"/>
</Stackpanel>
</GRID>
</WINDOW>

We set ListBox. issynchronizedwithcurrentitem to true, so that when we change the ListBox selection item, the following three tags will be automatically synchronized to the information of the selected record.

Of course, we can also use multiple listboxes, which affect each other as the real Master/Slave table displays.

<Window X: class = "learn. WPF. window1"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: My = "CLR-namespace: Learn. WPF"
Title = "window1">
<Window. Resources>
<My: personallist X: Key = "personals">
<My: Personal Name = "Tom" age = "10" Sex = "male"/>
<My: Personal Name = "Mary" age = "15" Sex = "female"/>
<My: Personal Name = "Jack" age = "12" Sex = "male"/>
</My: personallist>
</Window. Resources>
<Grid>
<Stackpanel>
<ListBox X: Name = "listbox1"
Itemssource = "{binding source = {staticresource personals }}"
Displaymemberpath = "name"
Issynchronizedwithcurrentitem = "true">
</ListBox>

<ListBox X: Name = "listbox2"
Itemssource = "{binding source = {staticresource personals }}"
Displaymemberpath = "Age"
Issynchronizedwithcurrentitem = "true">
</ListBox>
</Stackpanel>
</GRID>
</WINDOW>

 

It must be noted that issynchronizedwithcurrentitem does not support multi-choice synchronization.

5. datacontext Shared Source

In
In the above example, we need to bind the same resource to multiple UI elements, and obviously write "{binding source = {staticresource
Personals} "is a very tedious and difficult modification method. WPF provides a data context )"
So that we can share a source object on multiple elements, just put it in the datacontext attribute of the parent element.

<Window X: class = "learn. WPF. window1"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: My = "CLR-namespace: Learn. WPF"
Title = "window1">
<Window. Resources>
<My: personallist X: Key = "personals">
<My: Personal Name = "Tom" age = "10" Sex = "male"/>
<My: Personal Name = "Mary" age = "15" Sex = "female"/>
<My: Personal Name = "Jack" age = "12" Sex = "male"/>
</My: personallist>
</Window. Resources>
<Grid>
<Stackpanel datacontext = "{staticresource personals}">
<ListBox X: Name = "listbox1"
Itemssource = "{binding }"
Displaymemberpath = "name"
Issynchronizedwithcurrentitem = "true">
</ListBox>

<Label X: Name = "lblname" content = "{binding Path = Name}"/>
<Label X: Name = "lblage" content = "{binding Path = age}"/>
<Label X: Name = "lblsex" content = "{binding Path = sex}"/>
</Stackpanel>
</GRID>
</WINDOW>

When the source attribute is not specified for the binding extension flag, it will automatically find the data context of the parent element.

Of course, we can also do the same thing in the code.

Window1.xaml

<Window X: class = "learn. WPF. window1"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: My = "CLR-namespace: Learn. WPF"
Title = "window1">
<Grid>
<Stackpanel X: Name = "stackpanel1">
<ListBox X: Name = "listbox1"
Itemssource = "{binding }"
Displaymemberpath = "name"
Issynchronizedwithcurrentitem = "true">
</ListBox>

<Label X: Name = "lblname" content = "{binding Path = Name}"/>
<Label X: Name = "lblage" content = "{binding Path = age}"/>
<Label X: Name = "lblsex" content = "{binding Path = sex}"/>
</Stackpanel>
</GRID>
</WINDOW>

Window1.xaml. CS

Public partial class window1: Window
{
Public window1 ()
{
Initializecomponent ();

VaR list = new personallist {
New personal {name = "Tom", age = 10, sex = "male "},
New personal {name = "Mary", age = 15, sex = "female "},
New personal {name = "Jack", age = 12, sex = "male "},
};

This. stackpanel1.datacontext = List;
}
}

From the above example, we can see that datacontext makes data and UI separation more flexible, because datacontext can span multi-level parent elements. For example, we can directly set the data source to window. datacontext.

Public partial class window1: Window
{
Public window1 ()
{
......

This. datacontext = List;
}
}

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.