我有ItemsControl
绑定到DataContext
。当DataContext
发生变化时,我想要ItemsControl
的动画高度变化。我尝试为DataContextChanged
指定ItemsControl
事件:
<ItemsControl x:Name="items" ItemsSource="{Binding}" ItemTemplate="{StaticResource LocationTemplate}" DataContextChanged="Items_DataContextChanged">
在处理程序中,我尝试为高度创建DoubleAnimation
。但是,我不知道如何指定From
和To
属性。有人可以帮忙吗?谢谢!
答案 0 :(得分:1)
我能够通过将ItemsControl
包裹在Canvas
中来创建(我认为)您所追求的效果:
<Canvas x:Name="ClippingContainer" Background="Aquamarine" HorizontalAlignment="Center" VerticalAlignment="Center" ClipToBounds="True">
<ItemsControl x:Name="ICont" ItemsSource="{Binding}" SizeChanged="ItemsControl_SizeChanged"/>
</Canvas>
然后通过动画父ItemsControl.SizeChanged
的{{1}}和Height
属性来回复Width
事件。
Canvas
注意:这可以轻松转换为自己的private void ItemsControl_SizeChanged(object sender, SizeChangedEventArgs e)
if (double.IsNaN(ClippingContainer.Height))
{
ClippingContainer.Height = e.NewSize.Height;
}
else
{
ClippingContainer.BeginAnimation(FrameworkElement.HeightProperty, new DoubleAnimation(e.NewSize.Height, new Duration(TimeSpan.FromSeconds(1))));
}
if (double.IsNaN(ClippingContainer.Width))
{
ClippingContainer.Width = e.NewSize.Width;
}
else
{
ClippingContainer.BeginAnimation(FrameworkElement.WidthProperty, new DoubleAnimation(e.NewSize.Width, new Duration(TimeSpan.FromSeconds(1))));
}
}
。在这样做时,您可以覆盖UserControl
并强制布局传递以重绘动画MeasureOverride
所属的任何父布局容器。
我希望你觉得这很有用。
答案 1 :(得分:0)
private void MyItemsControl_DataContextChanged(object Sender, DependencyPropertyChangedEventArgs e)
{
BeginAnimation(HeightProperty, New DoubleAnimation(500.0, New Duration(Timespan.FromMilliseconds(500))));
}
答案 2 :(得分:0)
private void Grid_SizeChanged_1(object sender, SizeChangedEventArgs e)
{
e.Handled = true;
BeginAnimation(HeightProperty, new System.Windows.Media.Animation.DoubleAnimation(e.NewSize.Height, new Duration(TimeSpan.FromSeconds(1))));
}