目前我有以下代码在DataGrid单元格的值发生变化时闪存:
<TextBlock Background="White" Text={Binding Date, NotifyOnTargetUpdated=True} >
<EventTrigger RoutedEvent="Binding.TargetUpdated" >
<BeginStoryboard>
<Storyboard>
<ColorAnimation AutoReverse="True" From="#1F1F1F" To="#FFFF88" Duration="0:0:0.2" Storyboard.TargetProperty="Background.Color" FillBehavior="Stop" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock>
现在我只有在viewmodel的布尔值为true时才需要执行此动画。我怎么能做到这一点?
编辑:扩展样本
感谢。
答案 0 :(得分:2)
感谢XAML家伙在另一个论坛上的回答。这是问题的答案,解决方案是使用以下附加行为:
class AttachedBehaviours
{
public static bool GetAllowTargetUpdated(DependencyObject obj)
{
return (bool)obj.GetValue(AllowTargetUpdatedProperty);
}
public static void SetAllowTargetUpdated(DependencyObject obj, bool value)
{
obj.SetValue(AllowTargetUpdatedProperty, value);
}
// Using a DependencyProperty as the backing store for AllowTargetUpdated. This enables animation, styling, binding, etc...
public static readonly DependencyProperty AllowTargetUpdatedProperty =
DependencyProperty.RegisterAttached("AllowTargetUpdated", typeof(bool), typeof(AttachedBehaviours), new UIPropertyMetadata(false, PropChanged));
static void PropChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var ele = obj as FrameworkElement;
var be = ele.GetBindingExpression(TextBlock.TextProperty);
if (be == null) return;
var b = be.ParentBinding;
var newBinding = new Binding(b.Path.Path);
newBinding.NotifyOnTargetUpdated = (bool)e.NewValue;
ele.SetBinding(TextBlock.TextProperty, newBinding);
}
}
这是XAML的用法:
<TextBlock Background="White" Text="{Binding Date}" local:AttachedBehaviours.AllowTargetUpdated="{Binding EnableAnimations}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,10,0,0" >
答案 1 :(得分:1)
你有没有试过这样的事情:
<ControlTemplate.Triggers>
<DataTrigger Value="false">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation AutoReverse="True" From="#1F1F1F" To="#FFFF88" Duration="0:0:0.2" Storyboard.TargetProperty="Background.Color" FillBehavior="Stop" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</ControlTemplate.Triggers>
答案 2 :(得分:1)
嗯,这就是我解决问题的方法。可能是我写过的最丑陋的代码之一,所以请随意提出更好的建议。
我创建了一个转换器,我使用支持/禁用动画的布尔值绑定到持续时间。像这样:
class AnimationEnablerConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool enableAnimation = (bool)value;
if (enableAnimation)
{
return new System.Windows.Duration(new TimeSpan(0, 0, 0, 0, 200));
}
else
{
return new System.Windows.Duration(new TimeSpan(0, 0, 0, 0, 0));
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
和xaml:
<ColorAnimation AutoReverse="True" From="#1F1F1F" To="#FFFF88" Duration="{Binding IsAnimationEnabled, Converter={StaticResource anim2}}" Storyboard.TargetProperty="Background.Color" FillBehavior="Stop" />