当ItemsControl的DataContext在WPF中更改时,动画高度更改

时间:2012-11-06 21:02:49

标签: c# wpf storyboard itemscontrol

我有ItemsControl绑定到DataContext。当DataContext发生变化时,我想要ItemsControl的动画高度变化。我尝试为DataContextChanged指定ItemsControl事件:

<ItemsControl x:Name="items" ItemsSource="{Binding}" ItemTemplate="{StaticResource LocationTemplate}" DataContextChanged="Items_DataContextChanged">

在处理程序中,我尝试为高度创建DoubleAnimation。但是,我不知道如何指定FromTo属性。有人可以帮忙吗?谢谢!

3 个答案:

答案 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))));
}