将项目添加到列表框时Animate WPF Datatemplate? [英] Animate WPF Datatemplate when item added to Listbox?

查看:152
本文介绍了将项目添加到列表框时Animate WPF Datatemplate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我有一个WPF Listbox绑定到一个ObservableCollection。每次将新项目添加到集合中时,相同的项目将自动添加到列表框中。
要显示列表框中的项目,我使用XAML数据表。



我想要做的是将项目添加到集合/列表框。
可以这样做吗?
作为datatemplate中的动画可能吗?我想我需要一个触发器来启动这个动画,但是当添加一个新的item / datatemplate时会触发什么触发器?

解决方案

我认为FrameworkElement.Loaded路由事件的事件触发器可以工作。例如:

 < DataTemplate DataType ={x:Type l:Foo}> 
< Button x:Name =ButtonContent ={Binding Path = Bar}>
< Button.Background>
< SolidColorBrush x:Name =ButtonBrushColor =Tan/>
< /Button.Background>
< / Button>
< DataTemplate.Triggers>
< EventTrigger RoutedEvent =FrameworkElement.LoadedSourceName =Button>
< BeginStoryboard>
< Storyboard>
< ColorAnimation Storyboard.TargetName =ButtonBrushStoryboard.TargetProperty =ColorTo =Red/>
< / Storyboard>
< / BeginStoryboard>
< / EventTrigger>
< /DataTemplate.Triggers>
< / DataTemplate>


In my project I have a WPF Listbox bound to an ObservableCollection. Every time I add a new item to the Collection the same item is added to the Listbox automaticly. To display the items in the Listbox I use a XAML Datatemplate.

What I want to do is animate an item once when it is added to the Collection/Listbox. Can this be done? As animation in the datatemplate maybe? I guess I need a trigger somehow to start this animate but what trigger is fired when a new item/datatemplate is added?

解决方案

I think an event trigger for the FrameworkElement.Loaded routed event could work. For example:

<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>

这篇关于将项目添加到列表框时Animate WPF Datatemplate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆