【问题标题】:Animate WPF Datatemplate when item added to Listbox?将项目添加到列表框时为 WPF 数据模板设置动画?
【发布时间】:2009-03-12 09:20:43
【问题描述】:

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

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

【问题讨论】:

    标签: wpf data-binding xaml animation datatemplate


    【解决方案1】:

    我认为 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>
    

    【讨论】:

      猜你喜欢
      • 2011-08-30
      • 2017-10-28
      • 2011-10-18
      • 2011-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      相关资源
      最近更新 更多