如何在 WPF 中有条件地执行事件触发器



目前我有以下代码在 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>

现在,仅当视图模型的布尔值为 true 时,我才需要执行此动画。我怎样才能做到这一点?

编辑:扩展示例

谢谢。

感谢 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"  >

你有没有试过这样的事情:

<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>

嗯,这就是我解决问题的方式。可能是我写过的最丑陋的代码之一,所以请随时提出更好的建议。

创建了一个转换器,我使用启用/禁用动画的布尔值绑定到持续时间。像这样:

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" />

最新更新