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

Source: Internet
Author: User
Tags xpath

The This.listBoxStudents.DisplayMemberPath of the previous article = "Name"; The XAML code then changes to:

<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 ">
<ListBox.ItemTemplate>
<DataTemplate>
<stackpanel orientation= "Horizontal" >
<textblock text= "{Binding Id}" width= "+"/>
<textblock text= "{Binding Name}" width= "/>
<textblock text= "{Binding age}" width= "/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>


When a collection type is a itemsource of a list control, it is generally considered to use observablecollection<t> instead of list<t> because observablecollection<t> The class implements the INotifyCollectionChanged and INotifyPropertyChanged interfaces, which immediately notifies the list control that the change of the set is displayed, and the changes are immediately apparent.

When using XML data as the source of a binding, use XPath instead of the Path property to specify the source of the data.

Xml:

<?xml version= "1.0" encoding= "Utf-8"?>
<StudentList>
<student Id = "1" >
<Name>Tim</Name>
</Student>
<student Id = "2" >
<Name>Tom</Name>
</Student>
<student Id = "3" >
<Name>Vena</Name>
</Student>
<student Id = "4" >
<Name>Emily</Name>
</Student>
</StudentList>

Xaml:

<stackpanel background= "LightBlue" >
<listview x:name= "listviewstudent" height= "margin=" 5 ">
<ListView.View>
<GridView>
<gridviewcolumn header= "Id" width= "displaymemberbinding=" {Binding [email protected]} "/>
<gridviewcolumn header= "Name" width= "displaymemberbinding=" {Binding xpath=name} "/>
</GridView>
</ListView.View>
</ListView>
<button content= "Load" click= "Button_Click" height= "margin=" 5 "/>
</StackPanel>

C # code:

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

private void Button_Click (object sender, RoutedEventArgs e)
{
XmlDocument doc = new XmlDocument ();
Doc. Load ("Rawdata.xml");

XmlDataProvider XDP = new XmlDataProvider ();
Xdp. Document = doc;
Xdp. XPath = @ "/studentlist/student";
This.listViewStudent.DataContext = XDP;
This.listViewStudent.SetBinding (Listview.itemssourceproperty, New Binding ());
}
}


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

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

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.