Including a guard in a WPF event trigger. Is it possible?

I currently have

<ContentControl Grid.Column="2" Grid.Row="3" > <ContentControl.Triggers> <EventTrigger RoutedEvent="UIElement.MouseEnter"> <BeginStoryboard Storyboard="{StaticResource ShakeStatorMinorRadiusEdit}"/> </EventTrigger> </ContentControl.Triggers> ... <snip> ... </ContentControl> 

and

  <Grid.Resources> <Storyboard x:Key="ShakeStatorMinorRadiusEdit"> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="StatorMinorRadiusEdit" Storyboard.TargetProperty="RenderTransform.X" RepeatBehavior="5x" > <EasingDoubleKeyFrame KeyTime="0:0:0.05" Value="0"/> <EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="3"/> <EasingDoubleKeyFrame KeyTime="0:0:0.15" Value="0"/> <EasingDoubleKeyFrame KeyTime="0:0:0.20" Value="-3"/> <EasingDoubleKeyFrame KeyTime="0:0:0.25" Value="0"/> </DoubleAnimationUsingKeyFrames> </Storyboard> </Grid.Resources> 

enter image description here

The idea is that when the mouse enters the yellow highlighted control on the left, the yellow control on the right will tremble. The control on the right has x: Name = StatorMinorRadiusEdit Still works so well above.

Now I want to add one more complication. I just want the animation to start if the value in my view model is RotorLobes == 1. In an imaginary world, I would.

 <ContentControl Grid.Column="2" Grid.Row="3" > <ContentControl.Triggers> <EventTrigger RoutedEvent="UIElement.MouseEnter"> <If Property="{Binding RotorLobes}" Value="1"/> <BeginStoryboard Storyboard="{StaticResource ShakeStatorMinorRadiusEdit}"/> </EventTrigger> </ContentControl.Triggers> ... <snip> ... </ContentControl> 

In the real world, I have no idea how to achieve this.

+8
c # wpf eventtrigger
source share
1 answer

You can try moving from EventTrigger to MultiTrigger and use the IsMouseOver property rather than the MouseEnter event. However, as you discovered, Storyboards in styles does not allow you to specify a TargetName, so it is important to transfer triggers to the target.

In the example below, the Rotor value is hard-coded to 1, and you need to skip the RenderTransform namespace in TargetProperty correctly or get an exception at runtime.

 <Grid> <Grid.Resources> <Storyboard x:Key="ShakeStatorMinorRadiusEdit"> <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="RenderTransform.(TranslateTransform.X)" RepeatBehavior="5x"> <EasingDoubleKeyFrame KeyTime="0:0:0.05" Value="0" /> <EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="3" /> <EasingDoubleKeyFrame KeyTime="0:0:0.15" Value="0" /> <EasingDoubleKeyFrame KeyTime="0:0:0.20" Value="-3" /> <EasingDoubleKeyFrame KeyTime="0:0:0.25" Value="0" /> </DoubleAnimationUsingKeyFrames> </Storyboard> </Grid.Resources> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <TextBlock Grid.Column="1">Rotor</TextBlock> <TextBlock Grid.Column="2">Stator</TextBlock> <TextBlock Grid.Column="0" Grid.Row="1">Lobes</TextBlock> <TextBlock Grid.Column="0" Grid.Row="2">Major Radius</TextBlock> <TextBlock Grid.Column="0" Grid.Row="3">Minor Radius</TextBlock> <TextBox Name="RotorLobes" Grid.Column="1" Grid.Row="1" Text="1" /> <TextBox Grid.Column="1" Grid.Row="2" /> <TextBox Name="MinorRadiusRotor" Background="Blue" Grid.Column="1" Grid.Row="3" /> <TextBox Grid.Column="2" Grid.Row="1" /> <TextBox Grid.Column="2" Grid.Row="2" /> <TextBox Name="MinorRadiusStator" Background="Green" Grid.Column="2" Grid.Row="3"> <TextBox.Style> <Style> <Style.Triggers> <MultiDataTrigger> <MultiDataTrigger.Conditions> <Condition Binding="{Binding ElementName=MinorRadiusRotor, Path=IsMouseOver}" Value="True" /> <Condition Binding="{Binding ElementName=RotorLobes, Path=Text}}" Value="1" /> </MultiDataTrigger.Conditions> <MultiDataTrigger.EnterActions> <BeginStoryboard Storyboard="{StaticResource ShakeStatorMinorRadiusEdit}" /> </MultiDataTrigger.EnterActions> </MultiDataTrigger> </Style.Triggers> </Style> </TextBox.Style> </TextBox> </Grid> 
+4
source share

All Articles