【问题标题】:WPF animation to expand-contract heightWPF动画扩展收缩高度
【发布时间】:2010-11-19 14:19:54
【问题描述】:

我想在图像上设置一个触发器,通过动画其高度属性来扩展和收缩 ItemsSource。

我有一个绑定到 ObservableCollection 的通用 ItemsSource,所以我不知道这个控件的总高度。

单击图像后,它应该更改其图像源字形以显示 itemssource 已展开。一旦再次单击,ItemsSource 应该开始从其当前高度收缩回 0 - 因为第一个动画可能没有完成。

目前我有以下图像触发器:

<Image Name="ExpandImage" Source="ArrowDown.png">
            <Image.Triggers>
                <EventTrigger RoutedEvent="Image.MouseLeftButtonDown">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="Height"                                                     
                                            Storyboard.TargetName="myItemsControl"
                                             From="0" To="300" Duration="0:0:2" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </Image.Triggers>
        </Image>

这很难看,因为它动画到固定高度 - 我需要它动画到 ItemsControl 的总(未知高度)。此外,它只支持单向(展开)动画。

我的 ItemsControl 很简单:

 <ItemsControl Name="myItemsControl" 
        ItemsSource="{Binding Items}"  Height="0" >
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <c:CustomUserControl/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

【问题讨论】:

    标签: wpf animation mvvm click


    【解决方案1】:

    您可以使用...好吧,ActualHeight 属性来获取项目控件的实际高度。问题是它不是依赖属性。但是,您可以使用附加行为来绑定它,就像 Kent Boogaart 在this answer 中所做的那样。

    这应该让您绑定到实际高度。我会留给你写附加的行为。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-31
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 2011-01-09
      • 2014-03-29
      • 2010-11-21
      相关资源
      最近更新 更多