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

我有一个绑定(bind)到 ObservableCollection 的通用 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>

最佳答案

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

这应该让你绑定(bind)到实际高度。我会让你来写附加的行为。 :)

关于WPF动画扩展契约(Contract)高度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1276860/

10-12 05:20
查看更多