& lt/ListBox & gt;& lt列表框?Name=\"lstBox2 \"?Height=\"300 \" >& " />
& lt!- ?XAML?-& gt;
& ltStackPanel & gt
& lt列表框?Name="lstBox1 "?Height="300 " >
& lt/ListBox & gt;
& lt列表框?Name="lstBox2 "?Height="300 " >
& lt列表框。ItemTemplate & gt
& lt數據模板& gt
& ltStackPanel?Orientation="Horizontal " >
& ltTextBlock?Text="{Binding?Path=Name} "?Width="200"/>
& ltTextBlock?Text="{Binding?Path=Spacing} "?Width="200 "?text alignment = " Right "/& gt;
& lt/stack panel & gt;
& lt/data template & gt;
& lt/ListBox。ItemTemplate & gt
& lt/ListBox & gt;
& lt/stack panel & gt;
//?上膛了?事件代碼
var?字體?=?字體。SystemFontFamilies
。選擇(s?= & gt?新的
{
名字?=?美國來源,
間距?=?美國行距
});
lstBox1。ItemsSource?=?字體;
lstBox2。ItemsSource?=?字體;
示例中的數據源(字體)是由?LINQ?生成的數據列表支持?IEnumerable?嚙合/界面
這些元素中的每壹個都是“匿名類型”的,並且具有兩個屬性:
名字?-?字體名稱?線
間距?-?行距?兩倍
然後設置好?列表框。ItemsSource?屬性將數據綁定到?列表框
在數據呈現的過程中
列表框?會自動成為壹個列表。字體?為中的每個元素分配壹個?ListBoxItem?目標
因為,ListBoxItem默認的數據模板為?(DataTemplate)?為了什麽?文本塊
因此,在沒有指定數據模板的情況下。ToString()?方法返回的文本
比如?listBox1?如所示
妳在線嗎?lstBox2?為了呈現元素的屬性值,定義了壹個數據模板。數據模板
然後放到模板裏?文本塊。文字?屬性綁定到元素的兩個屬性:
Text="{Binding?Path=Name} "
Text="{Binding?Path=Spacing} "
就這樣,列表元素?-& gt;?用戶界面對象之間的數據綁定
請參考MSDN:數據綁定幫助主題
/library/vstudio/ms 752039(v = vs . 100)。aspx