我想创建一个自定义的
我考虑从StackPanel派生出一个新类,但我需要知道要重写哪些方法。 最终解决方案:
另外定义并注册
StackPanel
,其中包含一个ReverseOrder
属性,我可以在声明时将其设置为true,使得StackPanel中元素的顺序与正常情况下相反(例如从底部到顶部或从右到左)。它需要能够动态反转。我考虑从StackPanel派生出一个新类,但我需要知道要重写哪些方法。 最终解决方案:
protected override System.Windows.Size ArrangeOverride( System.Windows.Size arrangeSize ) {
double x = 0;
double y = 0;
IEnumerable<UIElement> children = ReverseOrder ? InternalChildren.Cast<UIElement>().Reverse<UIElement>() : InternalChildren.Cast<UIElement>();
foreach ( UIElement child in children ) {
var size = child.DesiredSize;
child.Arrange( new Rect( new Point( x, y ), size ) );
if ( Orientation == Orientation.Horizontal )
x += size.Width;
else
y += size.Height;
}
if ( Orientation == Orientation.Horizontal )
return new Size( x, arrangeSize.Height );
else
return new Size( arrangeSize.Width, y );
}
另外定义并注册
ReverseOrder
,如果它发生变化,请调用UpdateLayout
。