WPF动画中的数据更改 [英] WPF animation on data change
本文介绍了WPF动画中的数据更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在WPF网格上列出一些对象.如果对象的数据发生变化,我想开始制作动画.
I'm working on a WPF grid listing some Objects. In case the data of the object changes I want to start an animation.
在下面的XAML代码摘录中
Below an excerpt of the XAML code is listed
<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的设置器如下所示:
Whenever a property changes, I fire a PropertyChangedEvent. For instance, the setter of the ID looks like:
set
{
m_ID = value;
OnPropertyChanged("ID");
}
OnPropertyChanged函数的外观如下:
Where the OnPropertyChanged function looks like:
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
但是,更改ID后,不会触发动画.关于如何解决此问题的任何想法?
However, when the ID is changed, the animation isn't triggered. Any idea on how to fix this?
推荐答案
您应使用事件触发器:
尝试这样的事情:
<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>
别忘了在模板中进行设置!
don't forget to set this within your template!
<TextField Name="idField" Text="{Binding ID, NotifyOnTargetUpdated=True}" />
这篇关于WPF动画中的数据更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文