当元素没有足够的空间时,我试图让堆叠面板在另一个上面溢出。目前我只有两个基本的堆叠面板:stackpanel
包含我的所有元素,以及overflow
,当元素无法适应时,元素会被移动到stackpanel
。这就是我在stackpanel
的SizeChanged事件中所做的事情:
//Move each item from the overflow back to the stackpanel to measure them again
foreach (var item in overflow.Children.OfType<FrameworkElement>())
{
overflow.Children.Remove(item);
stackpanel.Children.Insert(0, item);
}
foreach (var item in stackpanel.Children.OfType<FrameworkElement>())
{
// measure the box around the child
var _ItemBounds = item.TransformToVisual(null).TransformBounds(new Rect(0, 0, item.ActualWidth, item.ActualHeight));
// measure the grid containing the stackpanel
var _Intersection = stackpanelgrid.TransformToVisual(null).TransformBounds(new Rect(0, 0, stackpanelgrid.ActualWidth, stackpanelgrid.ActualHeight));
_Intersection.Intersect(_ItemBounds);
//If the item is partially or completely hidden:
if (!_Intersection.Equals(_ItemBounds))
{
stackpanel.Children.Remove(item);
overflow.Children.Insert(0, item);
}
}
我的问题是这些项目已正确移至溢出,但它们不会重新启动。问题是什么?