C# 更改自定义面板内的子项大小
目前,我正在尝试创建自己的自定义面板,例如WrapPanel、Grid等。 如何更改子项的大小-在C# 更改自定义面板内的子项大小,c#,wpf,panel,C#,Wpf,Panel,目前,我正在尝试创建自己的自定义面板,例如WrapPanel、Grid等。 如何更改子项的大小-在.measure()之后面板内部 我的代码如下所示: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; name
.measure()之后代码>面板内部
我的代码如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace CustomPanel
{
public class MyCustomPanel : Panel
{
public object Convertchild { get; private set; }
protected override Size MeasureOverride(Size availableSize)
{
Size childSize = new Size(availableSize.Width, double.PositiveInfinity);
foreach (UIElement elem in InternalChildren)
elem.Measure(childSize);
return availableSize;
}
protected override Size ArrangeOverride(Size finalSize)
{
child.Arrange(new Rect(x, y, child.DesiredSize.Width + 20, child.DesiredSize.Height + 20));
return finalSize;
}
}
}
也许这很重要。我想给孩子们一个这样的开始:
<local:MyCustomPanel>
<Rectangle Name="Rectangle_1" Height="200" Width="100" Fill="Green"/>
<Rectangle Name="Rectangle_2" Height="150" Width="100" Fill="Black"/>
<Rectangle Name="Rectangle_3" Height="100" Width="100" Fill="Orange"/>
<Rectangle Name="Rectangle_4" Height="100" Width="150" Fill="Yellow"/>
<Rectangle Name="Rectangle_5" Height="150" Width="150" Fill="Gray"/>
</local:MyCustomPanel>
您必须对排列覆盖
中的所有子元素调用排列()
。见@Clemens谢谢你的回答。那是我的错。我很不幸地删除了它。我已经编辑过了。宽度和高度不会再大20像素。当子元素的大小被明确设置时(就像您通过设置矩形的宽度和高度所做的那样),面板不会(也不应该)更改其子元素的大小。@Clemens我想创建类似于包装的东西。所有项目都应该有一个起始值。行宽度的其余部分应划分为其他元素。所以这是不可能的?WrapPanel不会改变其子对象的大小。此外,如果面板应调整其子元素的大小(如网格),则不应设置其宽度和高度。