代码
MyPanelParent.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; namespace MyPanels { public class MyPanelParent : Panel { /// <summary> /// /// </summary> /// <param name="availableSize">1、UIElement Measure()方法 根据元素的Clip、Visibility生成constraintSize,然后传递给 FrameworkElement 的MeasureCore()方法 2、</param> /// <returns></returns> protected override Size MeasureOverride(Size availableSize) { foreach (UIElement item in this.InternalChildren) { item.Measure(new Size(120, 120));//这里是入口 } return availableSize; } protected override Size ArrangeOverride(Size finalSize) { double x = 100; foreach (UIElement item in this.InternalChildren) { item.Arrange(new Rect(x, 0, item.DesiredSize.Width, item.DesiredSize.Height)); x += item.DesiredSize.Width; } return finalSize; } } }
MyPanel.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; namespace MyPanels { public class MyPanel : Panel { protected override System.Windows.Size MeasureOverride(System.Windows.Size availableSize) { foreach (UIElement item in this.InternalChildren) { item.Measure(availableSize); } return new Size(50, 50);//MyPanel 返回它期望的大小 } protected override System.Windows.Size ArrangeOverride(System.Windows.Size finalSize) { double xCordinate = 0; foreach (UIElement item in this.InternalChildren) { item.Arrange(new Rect(new Point(xCordinate, 0), item.DesiredSize)); xCordinate += item.DesiredSize.Width; } return finalSize; } } }
效果
具体过程分析
标签:控件,自定义,Windows,System,item,new,using,MyParent,Size From: https://www.cnblogs.com/cdaniu/p/16937284.html