Wpf 如何使用按钮填充ItemsControl

Wpf 如何使用按钮填充ItemsControl,wpf,xaml,user-interface,itemscontrol,Wpf,Xaml,User Interface,Itemscontrol,我需要用可点击按钮的单元格填充面板,如下所示: 这已在矩形中完成: <ItemsControl ItemsSource="{Binding Path=Cells}"> <ItemsControl.ItemTemplate> <DataTemplate> <Rectangle> <Rectangle.Fill>

我需要用可点击按钮的单元格填充面板,如下所示:

这已在矩形中完成:

<ItemsControl ItemsSource="{Binding Path=Cells}">
      <ItemsControl.ItemTemplate>
          <DataTemplate>
              <Rectangle>
                  <Rectangle.Fill>
                      <SolidColorBrush Color="Red"></SolidColorBrush>
                  </Rectangle.Fill>
              </Rectangle>
          </DataTemplate>
      </ItemsControl.ItemTemplate>
      <ItemsControl.ItemsPanel>
          <ItemsPanelTemplate>
               <commonControls:UniformGrid ElementsGap="5" HorizontalCount="{Binding Path=CellsInRow}" />
          </ItemsPanelTemplate>
      </ItemsControl.ItemsPanel>
  </ItemsControl>

但我需要用按钮来做。我正在尝试:

<ItemsControl ItemsSource="{Binding Path=Cells}">
      <ItemsControl.ItemTemplate>
          <DataTemplate>
              <Button>
                  <Button.Background>
                      <SolidColorBrush Color="Red"></SolidColorBrush>
                  </Button.Background>
              </Button>
          </DataTemplate>
      </ItemsControl.ItemTemplate>
      <ItemsControl.ItemsPanel>
          <ItemsPanelTemplate>
               <commonControls:UniformGrid ElementsGap="5" HorizontalCount="{Binding Path=CellsInRow}" />
          </ItemsPanelTemplate>
      </ItemsControl.ItemsPanel>
  </ItemsControl>

但似乎是这样的:

如何使用按钮从第一张图像中提取单元格?

UniformGrid的代码如下:

 public class UniformGrid : Panel
{
    public static readonly DependencyProperty HorizontalCountProperty =
        DependencyProperty.Register("HorizontalCount", typeof (int), typeof (UniformGrid),
                                    new PropertyMetadata(default(int)));

    public int HorizontalCount
    {
        get { return (int) GetValue(HorizontalCountProperty); }
        set { SetValue(HorizontalCountProperty, value); }
    }

    public static readonly DependencyProperty ElementsGapProperty =
        DependencyProperty.Register("ElementsGap", typeof (double), typeof (UniformGrid),
                                    new PropertyMetadata(default(double)));

    public double ElementsGap
    {
        get { return (double) GetValue(ElementsGapProperty); }
        set { SetValue(ElementsGapProperty, value); }
    }

    protected override Size MeasureOverride(Size availableSize)
    {
        return new Size();
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
        if (Children != null && Children.Count != 0)
        {
            var squareSideForElement = (finalSize.Width - (HorizontalCount - 1)*ElementsGap)/HorizontalCount;
            var sizeOfElement = new Size(squareSideForElement, squareSideForElement);
            for (var i = 0; i < Children.Count; i++)
            {
                var rowIndex = i%HorizontalCount;
                var columnIndex = i/HorizontalCount;
                var resultPoint = new Point
                    {
                        X = rowIndex*(squareSideForElement + ElementsGap),
                        Y = columnIndex*(squareSideForElement + ElementsGap)
                    };
                Children[i].Arrange(new Rect(resultPoint, sizeOfElement));

            }

        }
        return finalSize;
    }
}
公共类UniformGrid:面板
{
公共静态只读从属属性HorizontalCountProperty=
DependencyProperty.Register(“HorizontalCount”、typeof(int)、typeof(UniformGrid),
新属性元数据(默认值(int));
公共整数水平计数
{
获取{return(int)GetValue(HorizontalCountProperty);}
set{SetValue(HorizontalCountProperty,value);}
}
公共静态只读从属属性元素Approperty=
从属属性寄存器(“ElementsGap”、typeof(double)、typeof(UniformGrid),
新属性元数据(默认值(双精度));
公共双元素地图
{
获取{return(double)GetValue(ElementsGapProperty);}
set{SetValue(elementsApproperty,value);}
}
受保护的覆盖尺寸测量覆盖(尺寸可用尺寸)
{
返回新的大小();
}
受保护的替代尺寸排列替代(尺寸最终化)
{
if(Children!=null&&Children.Count!=0)
{
var squareSideForElement=(finalSize.Width-(HorizontalCount-1)*ElementsGap)/HorizontalCount;
var sizeOfElement=新尺寸(squareSideForElement,squareSideForElement);
for(var i=0;i
您的度量值必须调用每个孩子。请阅读中的备注部分,特别是以下注释:

在此过程中,元素应在每个子元素上调用Measure, 否则子元素的大小或排列将不正确

您至少应该这样做:

protected override Size MeasureOverride(Size availableSize)
{
    foreach (UIElement element in InternalChildren)
    {
        element.Measure(availableSize);
    }

    return new Size();
}
如果要确保每个子对象都计算其最大首选大小,可以传递无限宽和无限高以进行测量:

protected override Size MeasureOverride(Size availableSize)
{
    availableSize = new Size(double.PositiveInfinity, double.PositiveInfinity);

    foreach (UIElement element in InternalChildren)
    {
        element.Measure(availableSize);
    }

    return new Size();
}

你知道WPF里已经有一个了吗?谢谢你,克莱门斯!但现在看来是这样的:。矩形(正方形)怎么样?不知道。计算布局的是您的代码。你也许应该在你的数据模板中设置按钮的宽度和高度。是的!我用电脑处理它。谢谢大家的关注!