gpt4 book ai didi

c# - 如何在 WPF 中有条件地执行 EventTrigger?

转载 作者:太空狗 更新时间:2023-10-30 01:23:53 27 4
gpt4 key购买 nike

目前我有以下代码来在值更改时刷新 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>

现在我只需要在 View 模型的 bool 值为真时执行此动画。我怎样才能做到这一点?

编辑:扩展样本

谢谢。

最佳答案

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

关于c# - 如何在 WPF 中有条件地执行 EventTrigger?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10935104/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com