在自定义面板中更改子项目大小

时间:2017-06-20 07:33:48

标签: 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;

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>

0 个答案:

没有答案