我已将此事件添加到 StackPanel,以便在向 StackPanel 添加新项目时显示漂亮的动画:
expandableStack.SizeChanged += (s, e) =>
{
DoubleAnimation expand = new DoubleAnimation();
expand.Duration = TimeSpan.FromMilliseconds(250);
expand.From = e.PreviousSize.Height;
expand.To = e.NewSize.Height;
expandableStack.BeginAnimation(HeightProperty, expand);
};
如果新大小大于以前的大小,它会很好,但如果它更小(当我删除项目时)StackPanel 不会改变它的大小,因此事件 SizeChanged 不会触发。
我怎样才能让 StackPanel 适应内容?或者,我如何在 StackPanel 中检索我的项目的大小,我已经尝试了所有 Size/Height 属性,但没有一个代表:
MessageBox.Show("Height: " + expandableStack.Height.ToString());
MessageBox.Show("ActualHeight: " + expandableStack.ActualHeight.ToString());
MessageBox.Show("Render size: " + expandableStack.RenderSize.Height.ToString());
MessageBox.Show("ViewportHeight size: " + expandableStack.ViewportHeight.ToString());
MessageBox.Show("DesiredSize.Height size: " + expandableStack.DesiredSize.Height.ToString());
MessageBox.Show("ExtentHeight size: " + expandableStack.ExtentHeight.ToString());
MessageBox.Show("VerticalOffset size: " + expandableStack.VerticalOffset.ToString());