当向Listbox添加项目时,如何为WPF Datatemplate添加动画效果?

28
在我的项目中,我有一个绑定到ObservableCollection的WPF Listbox。每次向集合添加新项目时,相同的项目会自动添加到Listbox中。 为了显示Listbox中的项目,我使用XAML Datatemplate。
我想要做的是,在将项目添加到Collection/Listbox时,对该项目进行一次动画效果。 这可行吗? 可能在datatemplate中进行动画处理?我猜需要某种触发器来启动此动画,但是当添加新项目/datatemplate时触发哪个触发器?
1个回答

26

我认为 FrameworkElement.Loaded 路由事件的触发器可能会起作用。例如:

<DataTemplate DataType="{x:Type l:Foo}">
    <Button x:Name="Button" Content="{Binding Path=Bar}">
        <Button.Background>
            <SolidColorBrush x:Name="ButtonBrush" Color="Tan" />
        </Button.Background>
    </Button>
    <DataTemplate.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded" SourceName="Button">
            <BeginStoryboard>
                <Storyboard>
                    <ColorAnimation Storyboard.TargetName="ButtonBrush" Storyboard.TargetProperty="Color" To="Red" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接