Kei*_*ler 0 silverlight binding listbox silverlight-4.0
我想将Silverlight ListBox绑定到Dictionary<int, string>.我试过以下没有成功:
someListBox.ItemsSource = someItems;
Run Code Online (Sandbox Code Playgroud)
和
someListBox.ItemsSource = someItems.Values;
Run Code Online (Sandbox Code Playgroud)
假设字典在分配时完全填充,这两种方法都可以工作.在您的用户控件中只有这个: -
<ListBox x:Name="lst" />
Run Code Online (Sandbox Code Playgroud)
然后这段代码: -
var data = new Dictionary<int, string>();
data.Add(1, "Hello");
data.Add(2, "World");
lst.ItemsSource = data.Values;
Run Code Online (Sandbox Code Playgroud)
将显示两个字符串"Hello"和"World".
给ListBox一个模板: -
<ListBox x:Name="lst">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding Key}" Margin="5" />
<TextBlock Text="{Binding Value}" Margin="5" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
现在您可以分配字典本身: -
lst.ItemsSource = data;
Run Code Online (Sandbox Code Playgroud)
列表框显示键值对的集合.
| 归档时间: |
|
| 查看次数: |
4633 次 |
| 最近记录: |