I'm currently working on a Surface application where I need to call two different animations when a button is tapped.
How exactly should I be doing this? I'd like to do it declaratively if it's possible. Should I be using MultiTriggers for this, or?
Thanks in advance!
You can do this with an EventTrigger.
You can define the trigger in a FrameworkElement.Triggers property of any container of both the button and the animation targets.
<StackPanel
Orientation="Horizontal">
<StackPanel.Triggers>
<EventTrigger
SourceName="TheButton"
RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetName="LimeRect"
Storyboard.TargetProperty="Fill.Color"
To="Red" />
<ColorAnimation
Storyboard.TargetName="RedRect"
Storyboard.TargetProperty="Fill.Color"
To="Lime" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</StackPanel.Triggers>
<Button
x:Name="TheButton"
Content="Play" />
<Rectangle
x:Name="LimeRect"
Fill="Lime"
Width="50"
Height="50" />
<Rectangle
x:Name="RedRect"
Fill="Red"
Width="50"
Height="50" />
</StackPanel>
If there is a relative path to your targets, you can use Storyboard.Target="{Binding PathToTarget}" in place of Storyboard.TargetName="TargetName".
EDIT: (see comments)
If you are animating the button itself, you can put the triggers right in the button and you don't need any target names.
Example - Animating the size of a ToggleButton:
<ToggleButton
Content="Toggle"
Width="50"
Height="50">
<ToggleButton.Triggers>
<EventTrigger
RoutedEvent="ToggleButton.Checked">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Duration="00:00:00.2"
Storyboard.TargetProperty="Width"
To="100" />
<DoubleAnimation
Duration="00:00:00.2"
Storyboard.TargetProperty="Height"
To="100" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger
RoutedEvent="ToggleButton.Unchecked">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Duration="00:00:00.2"
Storyboard.TargetProperty="Width"
To="50" />
<DoubleAnimation
Duration="00:00:00.2"
Storyboard.TargetProperty="Height"
To="50" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ToggleButton.Triggers>
</ToggleButton>
Related
I have a rectangle i want to animate the height of:
<Rectangle Height="30" Width="10" Fill="Black" Margin="10,0,10,0">
<Rectangle.Triggers>
<EventTrigger RoutedEvent="Rectangle.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(Rectangle.Height)" From="50" To="10" Duration="0:0:1" RepeatBehavior="Forever"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
this works great only i want the initial starting height of the rectangle to be 30. this code overrides the height and puts it to 50 when beginning the animation
Just add a second animation.
<Rectangle Height="30"
Width="10"
Fill="Black"
Margin="10,0,10,0">
<Rectangle.Triggers>
<EventTrigger RoutedEvent="Rectangle.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(Rectangle.Height)"
From="30"
To="10"
Duration="0:0:1"
RepeatBehavior="1" />
<DoubleAnimation Storyboard.TargetProperty="(Rectangle.Height)"
From="50"
To="10"
Duration="0:0:1"
BeginTime="0:0:1"
RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
I think this code should be correct but the animation never starts. When the canvas is loaded I'd like the slider to go ahead until its maximum and then on reverse direction. I'm using Visual Studio.
<Canvas>
<Canvas.Triggers>
<EventTrigger RoutedEvent="Canvas.Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="Slider"
Storyboard.TargetProperty="Value"
From="50" To="100" AutoReverse="True" Duration="0:0:5"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Canvas.Triggers>
<Slider x:Name="Slider" Width="300" Height="30"
Maximum="100" Minimum="50"/>
</Canvas>
I want to make TextBlock like this
But,
aTextBlock.BeginAnimation(Button.MarginProperty, myDoubleAnimation);
get this error
'System.Windows.Media.Animation.DoubleAnimation' cannot be used to animate the 'Margin' property of type 'System.Windows.Thickness'.
I have test in Xaml and get same error:
<Border CornerRadius="8" Background="Red" Margin="352,173,214,368">
<TextBlock x:Name="TestB"
Text="{Binding ElementName=MTxt,Path=Text,NotifyOnSourceUpdated=True}"
Margin="0,0,0,0"
HorizontalAlignment="Center"
Foreground="White"
FontWeight="Bold"
FontSize="16">
<TextBlock.Triggers>
<EventTrigger RoutedEvent="TextBlock.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="TestB"
Storyboard.TargetProperty="(TextBlock.Margin)"
To="2"
Duration="0:0:1" AutoReverse="True"
RepeatBehavior="2" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>
</Border>
What is your idea?
You need to use ThicknessAnimation for this. DoubleAnimation is for properties of Double type.
<TextBlock.Triggers>
<EventTrigger RoutedEvent="TextBlock.Loaded">
<BeginStoryboard>
<Storyboard>
<ThicknessAnimation
Storyboard.TargetName="TestB"
Storyboard.TargetProperty="(TextBlock.Margin)"
To="2"
Duration="0:0:1" AutoReverse="True"
RepeatBehavior="2" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers>
I have added an effect to Viewbox as below:
<Viewbox Height="40" Width="40" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,12.5,65,0" >
<Viewbox.Effect>
<effects:RippleEffect Magnitude="0" Frequency="0" />
</Viewbox.Effect>
<Grid>
<Rectangle x:Name="MinimizeRect" RadiusX="7" RadiusY="7" StrokeThickness="2" Stroke="White" Fill="Transparent"/>
<Image Source="Images/Minimize.png" Margin="5"/>
</Grid>
<Viewbox.Triggers>
<EventTrigger RoutedEvent="Mouse.MouseEnter" >
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(Effect).Magnitude" To="0.1" Duration="0:0:0.3" AutoReverse="True" RepeatBehavior="Forever" />
<DoubleAnimation Storyboard.TargetProperty="(Effect).Frequency" To="40" Duration="0:0:0.3" AutoReverse="True" RepeatBehavior="Forever"/>
<ColorAnimation Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" To="DodgerBlue" Duration="0:0:0.3" Storyboard.TargetName="MinimizeRect"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="Mouse.MouseLeave" >
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(Effect).Magnitude" To="0" Duration="0:0:0.3" />
<DoubleAnimation Storyboard.TargetProperty="(Effect).Frequency" To="0" Duration="0:0:0.3" />
<ColorAnimation Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" To="Transparent" Duration="0:0:0.3" Storyboard.TargetName="MinimizeRect"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Viewbox.Triggers>
</Viewbox>
But my image and Stroke of rectangle does not remain white. They are grayed. What might be the problem here?
Here is the image.
In the above image Ripple effect is added to Minimize button. Any effect is not added to close button.
Try setting the RenderOptions.BitmapScalingMode to HighQuality like so:
<Image Source="Images/Minimize.png" Margin="5"
RenderOptions.BitmapScalingMode="HighQuality" />
I can't make this animation work:
<Ellipse Opacity="0.7" Width="150" Height="50" StrokeThickness="5">
<Ellipse.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever" AutoReverse="True">
<DoubleAnimation From="0" To="100" Duration="0:0:2" Storyboard.TargetProperty="LayoutTransform.Y" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Ellipse.Triggers>
</Ellipse>
Basically it's an Ellipse that translates from a starting point at Y to an ending point of Y. But basically, it doesn't move, and launches a mistake... Any ideas?
Animating the LayoutTransform will not move the object. What you are looking for is an animation of the RenderTransform property:
You have to put a TranslateTransform into Ellipse.RenderTransform and change Storyboard.TargetProperty:
<Ellipse Opacity="0.7" Width="150" Height="50" StrokeThickness="5" Stroke="Black">
<Ellipse.RenderTransform>
<TranslateTransform />
</Ellipse.RenderTransform>
<Ellipse.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever" AutoReverse="True">
<DoubleAnimation From="0" To="100" Duration="0:0:2"
Storyboard.TargetProperty="RenderTransform.Y" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Ellipse.Triggers>
</Ellipse>
To keep it simple you can put ellipsis in a canvas :
<Canvas>
<Ellipse Width="30" Height="30" Fill="Purple" Canvas.Top="0" >
<Ellipse.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded" >
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="0:0:5" AutoReverse="True" Storyboard.TargetProperty="(Canvas.Top)" To="100" RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Ellipse.Triggers>
</Ellipse>
</Canvas>