简体   繁体   English

每个ListBox.ItemTemplate> DataTemplate中都有不同的WPF故事板动画

[英]Different WPF Storyboard Animation in each ListBox.ItemTemplate>DataTemplate

I want to have different animations for each of a ListView item. 我希望每个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>

The above XAML code works fine for showing the same animation myAnimation for each listitem. 上面的XAML代码适用于为每个列表项显示相同的动画myAnimation But how I can achive it to show different animations (ie I have to define several Storyboards in Window.Resources), depending on a Binding Property of the listitem ViewModel? 但是,如何根据列表项 ViewModel 的Binding属性实现不同的动画(即,我必须在Window.Resources中定义几个Storyboard)呢?

EDIT: 编辑:

With the question linked below, I finally got it worked this way. 有了下面链接的问题,我终于以这种方式工作了。 Amazing! 惊人!

<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}" /> 

I'm not sure how accessible your model is for this approach but you should be able to use it to some measure. 我不确定您的模型对这种方法的可访问性,但是您应该可以在某种程度上使用它。 You can use a set of DataTriggers, each inheriting a storyboard: 您可以使用一组DataTriggers,每个都继承一个故事板:

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

I would use them within a ControlTemplate : 我会在ControlTemplate使用它们:

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

EDIT: 编辑:

This Question has a similar solution, you may not need the ControlTemplate at all. 这个问题有一个类似的解决方案,您可能根本不需要ControlTemplate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将属性绑定到创建的每个ListBox.ItemTemplate - Bind property to each ListBox.ItemTemplate created ListBox.ItemTemplate在DataTemplate内部具有自定义控件模板 - ListBox.ItemTemplate with a custom control template inside DataTemplate 如何在Windows Phone 7中以编程方式创建ListBox.Itemtemplate,datatemplate - How to create ListBox.Itemtemplate,datatemplate programatically in Windows Phone 7 WPF MVVM ListBox.ItemTemplate CheckBox IsChecked绑定 - WPF MVVM ListBox.ItemTemplate CheckBox IsChecked binding 在Listbox.ItemTemplate中找到控件(WPF C#) - Find control inside Listbox.ItemTemplate (WPF C#) 为每个项目XAML C#创建Listbox.ItemTemplate - Create Listbox.ItemTemplate for each item XAML C# 是否有一个RichTextBox等效于ListBox.ItemTemplate? - Is there a RichTextBox equivalent to a ListBox.ItemTemplate? 如何从ListBox.ItemTemplate DataTemplate内部调用Button命令以传递参数而不选择ListBox项目? - How do I call Button command from inside of ListBox.ItemTemplate DataTemplate, to pass parameter without selecting ListBox item? DataTrigger在ListBox.ItemTemplate中不起作用 - DataTrigger not working within a ListBox.ItemTemplate 如何使 ListBox.ItemTemplate 可重用/通用 - How to make a ListBox.ItemTemplate reusable/generic
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM