关于数据更改的WPF动画

时间:2015-01-13 16:10:22

标签: c# wpf xaml animation mvvm

我正在编写列出一些对象的WPF网格。如果对象的数据发生变化,我想开始动画。

下面列出了XAML代码的摘录

    <ListView Name="ListViewEmployeeDetails" Grid.Row="1" Margin="4,109,12,23"  ItemsSource="{Binding Products}"  >
        <ListView.View>
            <GridView x:Name="grdTest">
                <GridViewColumn Header="ID"  Width="100">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock x:Name="idField" Text="{Binding ID}" TextDecorations="Underline" Foreground="Blue"/>
                            <DataTemplate.Triggers>
                                <DataTrigger Binding="{Binding ID}">
                                    <DataTrigger.EnterActions>
                                        <BeginStoryboard>
                                            <Storyboard>
                                                <DoubleAnimation Duration="0:0:5" To="0.0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="idField"/>
                                            </Storyboard>
                                        </BeginStoryboard>
                                    </DataTrigger.EnterActions>
                                </DataTrigger>
                            </DataTemplate.Triggers>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"  Width="100" />
                <GridViewColumn Header="Price" DisplayMemberBinding="{Binding Price}" Width="100" />
                <GridViewColumn Header="Reliab" DisplayMemberBinding="{Binding Reliability}" Width="100" />
            </GridView>
        </ListView.View>
    </ListView>

每当属性发生变化时,我都会触发一个PropertyChangedEvent。例如,ID的setter看起来像:

        set
        {
            m_ID = value;
            OnPropertyChanged("ID");
        }

OnPropertyChanged函数的位置如下:

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion

但是,更改ID后,动画不会被触发。关于如何解决这个问题的任何想法?

1 个答案:

答案 0 :(得分:2)

您应该使用事件触发器:

尝试这样的事情:

<DataTemplate.Triggers>
    <EventTrigger RoutedEvent="Binding.TargetUpdated">
        <BeginStoryboard>
           <Storyboard>
              <DoubleAnimation Duration="0:0:5" To="0.0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="idField"/>
           </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
</DataTemplate.Triggers>

编辑:不要忘记在模板中设置它!

<TextField Name="idField" Text="{Binding ID, NotifyOnTargetUpdated=True}" />