3

有人可以为这个功能提供一个简单的例子吗?我试图实现的是这样的:

当我有 1 个孩子时 - 我想全屏显示;

当我有 2 个孩子时 - 我想拥有相同的高度和宽度,并在整行中显示每个孩子;

当我有 3 个孩子时,是这样的:

|*************|
|   1  |   2  |
|*************|
|      3      |
|*************|

当我有 4 个孩子时:

|*************|
|   1  |   2  |
|*************|
|   3  |   4  |
|*************|

我怎样才能做到这一点?

问候。

编辑 1:我认为这个面板可以继承 UniformGrid somwhow。

4

1 回答 1

8

似乎您不需要覆盖MeasureOverride,因为您的面板将占用所有可用空间,因此您只需要以如下方式实现放置逻辑ArrangeOverride

public class CustomPanel : Panel
{
    protected override Size ArrangeOverride(Size finalSize)
    {
        if (Children.Count == 1)
        {
            Children[0].Arrange(new Rect(new Point(0, 0), finalSize));
        }
        else if (Children.Count == 2)
        {
            var halfFinalSize = new Size(finalSize.Width, finalSize.Height/2);
            Children[0].Arrange(new Rect(new Point(0, 0), halfFinalSize));
            Children[1].Arrange(new Rect(new Point(0, finalSize.Height / 2), halfFinalSize));
        }
        else if (Children.Count == 3)
        {
            var halfFinalSize = new Size(finalSize.Width, finalSize.Height / 2);
            var quarterSize = new Size(finalSize.Width / 2, finalSize.Height / 2);

            Children[0].Arrange(new Rect(new Point(0, 0), halfFinalSize));
            Children[1].Arrange(new Rect(new Point(0, finalSize.Height / 2), quarterSize));
            Children[2].Arrange(new Rect(new Point(finalSize.Width / 2, finalSize.Height / 2), quarterSize));
        }
        else if (Children.Count == 4)
        {
            var quarterSize = new Size(finalSize.Width / 2, finalSize.Height / 2);
            Children[0].Arrange(new Rect(new Point(0, 0), quarterSize));
            Children[1].Arrange(new Rect(new Point(finalSize.Width / 2, 0), quarterSize));
            Children[2].Arrange(new Rect(new Point(0, finalSize.Height / 2), quarterSize));
            Children[3].Arrange(new Rect(new Point(finalSize.Width / 2, finalSize.Height / 2), quarterSize));
        }
        else if (Children.Count > 4)
        {
            //???
        }

        return base.ArrangeOverride(finalSize);
    }
}`

PS:确保Children count小于5

于 2013-10-30T09:18:04.013 回答