wpf - WPF中的复选框网格

标签 wpf grid itemscontrol

我有一个 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/

相关文章:

wpf - listBox.ItemContainerGenerator.ContainerFromItem() 将 null 返回到添加到列表框中的 NEW 项

c# - 我可以控制行为的产生吗?

wpf - 是否可以通过变换(或其他方式)复制形状

file - 在 ParaView 中显示高程网格

c# - 统一网格作为 ItemsControl 中所有项目的面板模板,第一个除外

c# - Wpf 图像控件阻止文件

objective-c - 如何将 NSWindow 对齐到网格

CSS 最小最大值() : prioritize max value

wpf - Items控制项,走出视野

binding - 如何在 WinRT 中的 ItemContainerStyle 中进行绑定(bind)?