项目添加到列表框时动画WPF数据模板?

Tal*_*ode 27 data-binding wpf animation xaml datatemplate

在我的项目中,我有一个绑定到ObservableCollection的WPF Listbox.每次我向Collection添加一个新项目时,相同的项目会自动添加到Listbox中.要显示列表框中的项目,我使用XAML Datatemplate.

我想要做的是在项目添加到Collection/Listbox时为项目设置动画.可以这样做吗?作为datatemplate中的动画可能吗?我想我需要一个触发器以某种方式启动这个动画,但是当添加一个新项目/ datatemplate时会触发什么触发器?

Dan*_*att 25

我认为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>
Run Code Online (Sandbox Code Playgroud)