【发布时间】:2015-10-16 23:05:57
【问题描述】:
我想知道是否可以在列表框数据模板中使用两个字典。我想在文本框中使用 Names 的值,并在复选框中使用 EnabledNames 的值。这可能吗?如果是这样,我将如何去做?
一些示例数据是:
Dictionary<string, string> Names = new Dictionary<string, string>()
{
{ "4EG25","John" },
{"923OT", "Joe" }
};
Dictionary<string, bool> EnabledNames = new Dictionary<string, bool>()
{
{ "4EG25",false },
{"923OT", true}
};
以及我想如何以如下方式使用:
<ListBox x:Name="listBox" HorizontalAlignment="Left" Height="359" VerticalAlignment="Top" Width="673" Margin="0,0,-0.333,-0.333" ItemsSource="{Binding Path=Names, Mode=OneWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Path=EnabledNames[ItemsSource.Key].Value}" />
<TextBlock Text="{Binding Path=ItemsSource.Value}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
【问题讨论】:
-
您为什么不想创建一个带有 Name 和 IsEnabled 属性的简单视图模型类?然后将 ListBox 绑定到此类的集合。
标签: c# wpf xaml data-binding listbox