在某些情况下,我需要设置动画行。我有一个Behaivor,它仅在某些属性更改时运行。我的问题是当行为终止时,我会失去以前所有的动画。在动画前后观看鼠标。此外,完成后,观察我何时单击该行。有解决办法吗?
public enum Events
{
Changed
}
public class AnimateBehavior : Behavior<TextBlock>
{
public Events Property { get; set; }
private ColorAnimation ColorAnimation { get; set; }
public Brush BackgroundBrush { get; set; }
protected override void OnAttached()
{
ColorAnimation = new ColorAnimation
{
AutoReverse = true,
To = Colors.LightBlue,
From = Colors.Transparent,
FillBehavior = FillBehavior.Stop,
RepeatBehavior = new RepeatBehavior(3),
Duration = new Duration(TimeSpan.FromMilliseconds(500))
};
AssociatedObject.DataContextChanged += OnDataContextChanged;
WireEvents();
}
private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (e.OldValue is INotifyPropertyChanged oldData)
{
oldData.PropertyChanged -= OnPropertyChanged;
}
WireEvents();
}
private void WireEvents()
{
if (AssociatedObject.DataContext is INotifyPropertyChanged currentData)
{
currentData.PropertyChanged += OnPropertyChanged;
}
}
private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (Property == Events.Changed)
{
var dataGridRow = AssociatedObject.TryFindParent<DataGridRow>();
//ColorAnimation.Completed += (s, x) =>
//{
// dataGridRow.Background = Brushes.Transparent;
//};
dataGridRow.Background =
(SolidColorBrush) new BrushConverter().ConvertFromString(dataGridRow.Background.ToString());
dataGridRow.Background?
.BeginAnimation(SolidColorBrush.ColorProperty, ColorAnimation);
}
}
protected override void OnDetaching()
{
if (AssociatedObject.DataContext is INotifyPropertyChanged currentData)
{
currentData.PropertyChanged -= OnPropertyChanged;
}
}
}
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding LastUpdated}">
<Interactivity:Interaction.Behaviors>
<customBehavior:AnimateBehavior Property="Changed" />
</Interactivity:Interaction.Behaviors>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>