4

目前,我有以下代码在 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 时,我才需要执行此动画。我怎样才能做到这一点?

编辑:扩展样本

谢谢。

4

3 回答 3

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"  >
于 2012-06-13T08:37:05.427 回答
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>
于 2012-06-07T19:38:08.807 回答
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" />
于 2012-06-08T15:43:53.147 回答