WPF(Binding of ItemsControl)
[html]
<window x:class="testofbindingitemscontrol.mainwindow"
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
title="mainwindow" height="350" width="525">
<stackpanel x:name="stackpanel" background="lightblue" >
<textblock text="studentid:" margin="5" />
<textbox x:name="textboxid" margin="5" />
<textblock text="studentlist:" fontweight="bold"
margin="5" />
<listbox x:name="listboxstudents"
height="110"
margin="5"
>
<listbox.itemtemplate>
<datatemplate>
<stackpanel orientation="horizontal" >
<textblock text="{binding path=id}" width="30" />
<textblock text="{binding path=name}" width="60" />
<textblock text="{binding path=age}" width="30" />
</stackpanel>
</datatemplate>
</listbox.itemtemplate>
</listbox>
</stackpanel>
</window>
<window x:class="testofbindingitemscontrol.mainwindow"
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
title="mainwindow" height="350" width="525">
<stackpanel x:name="stackpanel" background="lightblue" >
<textblock text="studentid:" margin="5" />
<textbox x:name="textboxid" margin="5" />
<textblock text="studentlist:" fontweight="bold"
margin="5" />
<listbox x:name="listboxstudents"
height="110"
margin="5"
>
<listbox.itemtemplate>
<datatemplate>
<stackpanel orientation="horizontal" >
<textblock text="{binding path=id}" width="30" />
<textblock text="{binding path=name}" width="60" />
<textblock text="{binding path=age}" width="30" />
</stackpanel>
</datatemplate>
</listbox.itemtemplate>
</listbox>
</stackpanel>
</window>
[csharp]
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.windows;
using system.windows.controls;
using system.windows.data;
using system.windows.documents;
using system.windows.input;
using system.windows.media;
using system.windows.media.imaging;
using system.windows.navigation;
using system.windows.shapes;
namespace testofbindingitemscontrol
{
/// <summary>
/// interaction logic for mainwindow.xaml
/// </summary>
public partial class mainwindow : window
{
public mainwindow()
{
initializecomponent();
list<student> stulist = new list<student>()
{
new student(){id = 0,name = "tim",age = 29},
new student(){id = 1,name = "tom",age = 28},
new student(){id = 2,name = "kyle",age = 27},
new student(){id = 3,name = "tony",age = 26},
new student(){id = 4,name = "vine",age = 25},
new student(){id = 5,name = "mike",age = 24}
};
// 为listbox设置binding
this.listboxstudents.itemssource = stulist;
//this.listboxstudents.displaymemberpath = "name";
binding binding = new binding("selecteditem.id")
{
source = this.listboxstudents
};
this.textboxid.setbinding(textbox.textproperty, binding);
}
}
public class student
{
public int id { get; set; }
public string name { get; set; }
public int age { get; set; }
}
}
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.windows;
using system.windows.controls;
using system.windows.data;
using system.windows.documents;
using system.windows.input;
using system.windows.media;
using system.windows.media.imaging;
using system.windows.navigation;
using system.windows.shapes;
namespace testofbindingitemscontrol
{
/// <summary>
/// interaction logic for mainwindow.xaml
/// </summary>
public partial class mainwindow : window
{
public mainwindow()
{
initializecomponent();
list<student> stulist = new list<student>()
{
new student(){id = 0,name = "tim",age = 29},
new student(){id = 1,name = "tom",age = 28},
new student(){id = 2,name = "kyle",age = 27},
new student(){id = 3,name = "tony",age = 26},
new student(){id = 4,name = "vine",age = 25},
new student(){id = 5,name = "mike",age = 24}
};
// 为listbox设置binding
this.listboxstudents.itemssource = stulist;
//this.listboxstudents.displaymemberpath = "name";
binding binding = new binding("selecteditem.id")
{
source = this.listboxstudents
};
this.textboxid.setbinding(textbox.textproperty, binding);
}
}
public class student
{
public int id { get; set; }
public string name { get; set; }
public int age { get; set; }
}
}