数据更改的 WPF 动画
WPF animation on data change
我正在处理列出一些对象的 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时,不会触发动画。知道如何解决这个问题吗?
您应该使用事件触发器:
尝试这样的事情:
<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}" />
我正在处理列出一些对象的 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时,不会触发动画。知道如何解决这个问题吗?
您应该使用事件触发器:
尝试这样的事情:
<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}" />