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)