[英]Bind Dictionary Collection to ListBox in Silverlight
我有一个词典集合,我需要将其绑定到Silverlight ListBox。 我无法使用Key值绑定到ListBox。 下面是示例代码。我在后面的代码中得到空记录Dictionary。
Dictionary<DayOfWeek, List<Book>> bookItem = new Dictionary<DayOfWeek, List<Book>>();
<ListBox x:Name="ListValues" ItemsSource="{Binding bookItem}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding BookName[Tuesday]}"></TextBlock>
<TextBlock Text="{Binding BookDesc[Tuesday]}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
IDictionary.GetEnumerator返回KeyValuePair<TKey, TValue>
的集合。 因此,为了绑定到对象,您需要使用KeyValuePair上的属性。 这是一个如何显示数据的示例。
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
</Grid>
<!-- Display the day of week in the first column -->
<TextBlock Text="{Binding Current.Key}"/>
<!-- Display the books in the second column -->
<GridView Grid.Column="1" ItemsSource="{Binding Current.Value}" />
</DataTemplate>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.