【问题标题】:Different WPF Storyboard Animation in each ListBox.ItemTemplate>DataTemplate每个 ListBox.ItemTemplate>DataTemplate 中的不同 WPF 故事板动画
【发布时间】:2017-12-07 20:44:33
【问题描述】:

我想为每个 ListView 项设置不同的动画。

<Window.Resources>
    <Storyboard x:Key="myAnimation" RepeatBehavior="Forever">
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Source" Duration="0:0:2">
            <DiscreteObjectKeyFrame KeyTime="0:0:0">
                <DiscreteObjectKeyFrame.Value>
                    <BitmapImage UriSource="/img/image1.png"/>
                </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>
            <DiscreteObjectKeyFrame KeyTime="0:0:1">
                <DiscreteObjectKeyFrame.Value>
                    <BitmapImage UriSource="/img/image2.png"/>
                </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>
</Window.Resources>

<ListBox  Name="SessionList" HorizontalContentAlignment="Stretch" >
    <ListBox.ItemTemplate>
        <DataTemplate>
           <Image x:Name="stateimage">
                <Image.Triggers>
                    <EventTrigger RoutedEvent="Loaded">
                        <BeginStoryboard Storyboard="{StaticResource myAnimation}">
                        </BeginStoryboard>
                    </EventTrigger>
                </Image.Triggers>
            </Image>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

上面的 XAML 代码可以很好地为每个列表项显示相同的动画myAnimation。但是我如何才能实现它以显示不同的动画(即我必须在 Window.Resources 中定义几个故事板),取决于 listitem ViewModel 的绑定属性

编辑:

通过下面链接的问题,我终于以这种方式工作了。太棒了!

<Window.Resources>
    <Storyboard x:Key="animLOAD" RepeatBehavior="Forever">...</Storyboard>
    <Storyboard x:Key="animPREPARED" RepeatBehavior="Forever">...</Storyboard>
    <Style x:Key="animStyle" TargetType="{x:Type Image}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=st, Mode=OneWay}" Value="LOAD">
                <DataTrigger.EnterActions>
                    <BeginStoryboard Storyboard="{StaticResource animLOAD}" />
                </DataTrigger.EnterActions>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=st, Mode=OneWay}" Value="PREPARED">
                <DataTrigger.EnterActions>
                    <BeginStoryboard Storyboard="{StaticResource animPREPARED}" />
                </DataTrigger.EnterActions>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<Image Style="{StaticResource animStyle}" /> 

【问题讨论】:

    标签: c# wpf xaml animation itemtemplate


    【解决方案1】:

    我不确定您的模型对于这种方法的可访问性,但您应该能够在某种程度上使用它。 您可以使用一组 DataTrigger,每个都继承一个故事板:

    <DataTrigger Binding="{Binding Property}" Value="SomeValue">                                            
        <DataTrigger.EnterActions>
            <BeginStoryboard Storyboard="{StaticResource Storyboard}"/>
        </DataTrigger.EnterActions>
    </DataTrigger>
    

    我会在 ControlTemplate 中使用它们:

    <ControlTemplate TargetType="Image">
        <ControlTemplate.Triggers>
            <!-- DataTriggers -->
        </ControlTemplate.Triggers>
    </ControlTemplate>
    

    编辑:

    This Question 也有类似的解决方案,您可能根本不需要ControlTemplate

    【讨论】:

    • 感谢您指出带有样式触发器的解决方案!我已经为此搜索了几天,但没有找到解决方案,我也没有想到为此使用 Styles。
    • 别担心!很高兴它可以帮助你。请接受我的回答,我需要我的 +rep 修复。
    猜你喜欢
    • 2017-03-02
    • 2016-07-02
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    • 2018-02-09
    • 2018-12-22
    • 2011-12-23
    • 1970-01-01
    相关资源
    最近更新 更多