什么是复选框列表绑定到选定值的列表的最简单方法 [英] What's the simplest way to bind a list of checkboxes to a list of checked values
问题描述
我 AvailableItems
的列表,我要显示为复选框列表,让用户可以选择生成哪些项目,然后将其存储在另一个列表名为 ItemsToGenerate
(我的列表实际上只是字符串列表)。
I have a list of AvailableItems
that I want to display as a list of checkboxes, so that users can pick which items to generate, which are then stored in another list called ItemsToGenerate
(my lists are actually just lists of strings).
显示与相应的复选框所有可用内容很简单:
Showing all available items with corresponding checkboxes is easy:
<ItemsControl ItemsSource="{Binding Path=AvailableItems}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
但现在我需要给每个Checkbox.IsChecked属性绑定,这样的事实,该项目是在 ItemsToGenerate
列表。我想制作的 ListContainmentToBoolConverter
是这样的:
But now I need to bind each Checkbox.IsChecked property, to the fact that the item is in the ItemsToGenerate
list. I thought of making a ListContainmentToBoolConverter
like this:
IsChecked="{Binding Path=ItemsToGenerate,
Converter={StaticResource ListContainmentToBoolConverter}}"
但是,这并不工作,因为我失去了一个 ConverterParameter
通过每个项目的价值,但我不能这样做,因为 ConverterParameter
不支持绑定。
But that doesn't work because I'm missing a ConverterParameter
to pass the value of each item, but I can't do that, because ConverterParameter
does not support binding.
任何想法?
推荐答案
我已经找到了解决我的问题。
I've found a solution to my problem.
我改变了的ItemsControl
到的ListBox
,并与我的<$的SelectedItems之间添加一个绑定C $ c。使用方法这里描述> ItemsToGenerate 集合。基本上,它可以让我用一个简单的附加属性同步任何自定义集合 ListBox.SelectedItems
。
I've changed my ItemsControl
to a ListBox
, and added a binding between the SelectedItems with my ItemsToGenerate
collection using the technique described here. It basically allows me to synchronize any custom collection to ListBox.SelectedItems
using a simple attached property.
<ListBox ItemsSource="{Binding AvailableItems}"
Behaviors:MultiSelectorBehaviours.SynchronizedSelectedItems=
"{Binding ItemsToGenerate}"
SelectionMode="Multiple"
Background="{x:Null}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding}"
Margin="3"
IsChecked="{Binding RelativeSource=
{RelativeSource Mode=FindAncestor,
AncestorType={x:Type ListBoxItem}},
Path=IsSelected}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListBox>
我仍然能够显示这是我最初想(复选框列表),通过添加一个数据模板给每个 ListBoxItem的
更改为复选框,并结合各 Checkbox.IsChecked
到 ListBoxItem.IsSelected
。
I'm still able to display this as I initially wanted (a list of checkboxes), by adding a data template to change each ListBoxItem
to a checkbox and binding each Checkbox.IsChecked
to ListBoxItem.IsSelected
.
我在我的应用程序,这对我来说是理想的解决方案那么多地方这种模式,因为现在我只需要指定一个附加属性,其余全部由数据绑定处理,我不需要任何额外的code。
I had this pattern in so many places in my application that this is the ideal solution for me, because now I just need to specify one attached property, and the rest is all handled by the data bindings, and I don't need any additional code.
这篇关于什么是复选框列表绑定到选定值的列表的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!