gpt4 book ai didi

c# - 面板 ArrangeOverride 和 MeasureOverride 方法

转载 作者:行者123 更新时间:2023-11-30 13:57:47 24 4
gpt4 key购买 nike

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

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

当我有 2 个 child 时 - 我希望具有相同的高度和宽度,并且每个 child 都显示在整行中;

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

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

当我有 4 个 child 时:

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

我怎样才能做到这一点?

问候。

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

最佳答案

似乎您不需要覆盖 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 计数小于 5

关于c# - 面板 ArrangeOverride 和 MeasureOverride 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19677936/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com