1

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

带长方形

这是用矩形完成的:

<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;
    }
}
4

1 回答 1

2

您的 MeasureOverride 必须对每个孩子调用Measure 。请阅读MeasureOverride中的备注部分,尤其是以下注释:

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

你至少应该这样做:

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

    return new Size();
}

如果您想确保每个孩子都计算其最大首选尺寸,您可以将无限的宽度和高度传递给 Measure:

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

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

    return new Size();
}
于 2012-12-18T11:37:59.440 回答