我有一个 WPF UserControl 的 datacontext 绑定(bind)到这样的类:
public class CheckBoxGridViewModel
{
public List<List<bool>> Checkboxes {get; set;}
}
我希望它显示一个复选框网格。我假设我可以使用 Itemscontrol,但不确切知道如何使用每行的动态列集来做到这一点。
This question似乎回答了我的,除了答案没有给出例子,我不知道如何写出来。
所以问题是,我将如何编写 xaml 来显示 Checkboxes 属性的复选框,以便它们排列在一个漂亮的网格中?
外部列表将是每一行,内部列表将是该行的每一列。
最佳答案
目前尚不清楚您是否期望每个内部列表的大小相同,但如果是,您可以使用简单的设置。使用带有单行/列 UniformGrids 的嵌套 ItemsControls 将为您提供均匀分布并自动处理任何大小的集合,而无需像使用 Grid 那样设置行和列定义:
<ItemsControl ItemsSource="{Binding Checkboxes}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsPanelTemplate>
<UniformGrid Rows="1"/>
</ItemsPanelTemplate>
</ItemsControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="1"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
关于wpf - WPF中的复选框网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4490989/