2

我有一个ItemsControl绑定到它的DataContext. 当变化时,我想要动画DataContext的高度变化。ItemsControl我试图指定一个DataContextChanged事件ItemsControl

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

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

4

3 回答 3

1

ItemsControl通过将a包装起来,我能够创建(我认为)您所追求的效果Canvas

<Canvas x:Name="ClippingContainer" Background="Aquamarine" HorizontalAlignment="Center" VerticalAlignment="Center" ClipToBounds="True">
    <ItemsControl x:Name="ICont" ItemsSource="{Binding}" SizeChanged="ItemsControl_SizeChanged"/>
</Canvas>

然后通过对 parent 的和属性进行ItemsControl.SizeChanged动画处理来响应事件。HeightWidthCanvas

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的任何父布局容器。ItemsControl

我希望你觉得这有帮助。

于 2012-11-07T15:01:06.523 回答
0
private void MyItemsControl_DataContextChanged(object Sender, DependencyPropertyChangedEventArgs e)
{
    BeginAnimation(HeightProperty, New DoubleAnimation(500.0, New Duration(Timespan.FromMilliseconds(500))));
}
于 2012-11-06T21:13:28.313 回答
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))));
}
于 2012-11-06T22:06:18.413 回答