dev*_*os1 9 .net c# wpf 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 );
}
Run Code Online (Sandbox Code Playgroud)
如果更改,还要定义并注册ReverseOrder和调用UpdateLayout.
gga*_*ber 12
您可以重新实现FrameworkElement.ArrangeOverride并在必要时以相反的顺序调用所有child.Arrange.
http://msdn.microsoft.com/en-us/library/system.windows.uielement.arrange.aspx
像这样的东西(未经测试):
double x = 0;
double y = 0;
var children = ReverseOrder ? InternalChildren.Reverse() : InternalChildren;
foreach (UIElement child in children)
{
var size = child.DesiredSize;
child.Arrange(new Rect(new Point(x, y), size));
if (Orientation == Horizontal)
x += size.Width;
else
y += size.Height;
}
Run Code Online (Sandbox Code Playgroud)
确保在更改ReverseOrder属性后调用UpdateLayout.
| 归档时间: |
|
| 查看次数: |
2930 次 |
| 最近记录: |