How to conditionally execute EventTrigger in WPF?

Currently, I have the following code to run a DataGrid cell when the value changes:

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

Now I need to perform this animation only if the boolean value of the viewmodel is true. How can i do this?

EDIT: expanded sample

Thanks.

+4
source share
3 answers

Thanks to the XAML guy for his answer on another forum. This is the answer to the question, the solution is to use the following attached behavior:

 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); } } 

And this is its use from XAML:

 <TextBlock Background="White" Text="{Binding Date}" local:AttachedBehaviours.AllowTargetUpdated="{Binding EnableAnimations}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,10,0,0" > 
+2
source

Have you tried something like this:

 <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> 
+1
source

Well, that’s how I solved the problem. This is probably one of the ugliest codes I've ever written, so feel free to suggest something better.

I created a converter that I associate with duration using a boolean that enables / disables the animation. Something like that:

 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 } 

And xaml:

 <ColorAnimation AutoReverse="True" From="#1F1F1F" To="#FFFF88" Duration="{Binding IsAnimationEnabled, Converter={StaticResource anim2}}" Storyboard.TargetProperty="Background.Color" FillBehavior="Stop" /> 
+1
source

Source: https://habr.com/ru/post/1416614/


All Articles