Configure WPF Trace in XAML

I currently have a set of buttons that I would like to set triggers so that each of them performs the same animation. Is there any way in XAML to pass the target into the storyboard so I don't have to rewrite the storyboard every time for each goal?

+4
source share
1 answer

If you do not set an explicit goal, the goal should be the element to which the animation applies. I would define a style with a trigger / animation on it and apply the style to those specific buttons that you want to show. For instance:

<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}"> <Style.Resources> <Storyboard x:Key="OnMouseEnterStoryboard"> <DoubleAnimation BeginTime="00:00:00" Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)" To="180" /> </Storyboard> <Storyboard x:Key="OnMouseLeaveStoryboard"> <DoubleAnimation BeginTime="00:00:00" Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)" To="0" /> </Storyboard> </Style.Resources> <Style.Triggers> <EventTrigger RoutedEvent="Mouse.MouseLeave"> <RemoveStoryboard BeginStoryboardName="OnMouseEnterStoryboard_BeginStoryboard"/> <BeginStoryboard x:Name="OnMouseLeaveStoryboard_BeginStoryboard" Storyboard="{StaticResource OnMouseLeaveStoryboard}"/> </EventTrigger> <EventTrigger RoutedEvent="Mouse.MouseEnter"> <BeginStoryboard x:Name="OnMouseEnterStoryboard_BeginStoryboard" Storyboard="{StaticResource OnMouseEnterStoryboard}"/> <RemoveStoryboard BeginStoryboardName="OnMouseLeaveStoryboard_BeginStoryboard"/> </EventTrigger> </Style.Triggers> <Setter Property="RenderTransformOrigin" Value="0.5,0.5"/> <Setter Property="RenderTransform"> <Setter.Value> <RotateTransform/> </Setter.Value> </Setter> </Style> 

And then on each button you want to behave as follows:

 <Button Style="{StaticResource MyButtonStyle}" ... /> 
+14
source

All Articles