Can't make an animation work WPF - wpf

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>

Related

Animate height from 50 to 10 but starting from 30 wpf

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>

While adding an effect to viewbox the object is grayed out

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

How do you keep an item at the same point on a Canvas in XAML as it grows or shrinks in size?

I have the following xaml that illustrates a circle that grows and shrinks in size. I want my center point of my circle to stay put on my canvas as the animation runs.
<Window.Triggers>
<EventTrigger RoutedEvent="Window.Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation RepeatBehavior="Forever" AutoReverse="True" From="10" To="360" Duration="0:0:3" Storyboard.TargetName="GrowMe" Storyboard.TargetProperty="Width"/>
<DoubleAnimation RepeatBehavior="Forever" AutoReverse="True" From="10" To="360" Duration="0:0:3" Storyboard.TargetName="GrowMe" Storyboard.TargetProperty="Height"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Window.Triggers>
<Canvas Width="640" Height="480">
<Ellipse Width="10" Height="10" Stroke="Red" Canvas.Left="80" Canvas.Top="31"/>
<Ellipse x:Name="GrowMe" Width="10" Height="10" Stroke="Cyan" Canvas.Left="205" Canvas.Top="203"/>
Here is an example of a button that does exactly this. Note the RenderTransformOrigin property.
<Button RenderTransformOrigin="0.5,0.5" Command="{Binding ClickCommand}" Cursor="Hand">
<Button.RenderTransform>
<ScaleTransform ScaleX="1" ScaleY="1" />
</Button.RenderTransform>
<Button.Template>
<ControlTemplate>
<Label>
<Label.Background>
<VisualBrush Visual="{Binding Path=Shape, Converter={StaticResource myTestConv}}" />
</Label.Background>
</Label>
</ControlTemplate>
</Button.Template>
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleX"
To="1.3" Duration="0:0:0.1" AccelerationRatio="0.5"/>
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleY"
To="1.3" Duration="0:0:0.1" AccelerationRatio="0.5"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleX"
To="1" Duration="0:0:0.5"/>
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleY"
To="1" Duration="0:0:0.5"/>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
<local:TooltipServiceEx.ToolTipEx>
<local:ToolTipEx
HideToolTipTimeout="{StaticResource TooltipTimeout}" />
</local:TooltipServiceEx.ToolTipEx>
<ToolTipService.ToolTip>
<ToolTip
Template="{StaticResource EventTooltipStyle}" />
</ToolTipService.ToolTip>
</Button>
You can do it easily animating ScaleTransform inside RenderTransform and set RenderTransformOrigin = "0.5, 0.5".
--EDIT--
Then your change your storyboard to following:
<Storyboard>
<DoubleAnimation RepeatBehavior="Forever" AutoReverse="True" From="10" To="180" Duration="0:0:3" Storyboard.TargetName="GrowMe" Storyboard.TargetProperty="Width"/>
<DoubleAnimation RepeatBehavior="Forever" AutoReverse="True" From="10" To="180" Duration="0:0:3" Storyboard.TargetName="GrowMe" Storyboard.TargetProperty="Height"/>
<DoubleAnimation RepeatBehavior="Forever" AutoReverse="True" By="-180" Duration="0:0:3" Storyboard.TargetName="GrowMe" Storyboard.TargetProperty="(Canvas.Left)"/>
<DoubleAnimation RepeatBehavior="Forever" AutoReverse="True" By="-180" Duration="0:0:3" Storyboard.TargetName="GrowMe" Storyboard.TargetProperty="(Canvas.Top)"/>
</Storyboard>
You need to devide your growth into half and add it to Width/Height and subtract it from Left/Top.

Calling two different animations from the same click event

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>

EventTrigger RoutedEvent in wpf xaml

I have a problem in wpf xaml and i'm pretty new on this so it may be something basic
i want to rotate a ellipse 360 degree
<Ellipse Name="test" Fill="Black" StrokeThickness="5" Margin="0,0,0,0" Height="66">
<Ellipse.Triggers>
<EventTrigger RoutedEvent="Ellipse.Loaded" SourceName="test">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="test"
Storyboard.TargetProperty="(Ellipse.RenderTransform).(RotateTransform.Angle)"
From="0"
To="360"
Duration="0:0:0.5"
RepeatBehavior="1x" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Ellipse.Triggers>
</Ellipse>
But nothing happens, what is wrong?
First of all. you will need the RotateTransform object in your ellipse:
<Ellipse.RenderTransform>
<RotateTransform x:Name="transform" />
</Ellipse.RenderTransform>
Change these properties in your Storyboard:
Storyboard.TargetName="transform"
Storyboard.TargetProperty="Angle"
And it should work!
You can also only change the TargetProperty, and leave the TargetName on test:
Storyboard.TargetName="test"
Storyboard.TargetProperty="RenderTransform.Angle"
That way, your RotateTransform object does not need a name!
Putting it all together:
<Ellipse Name="test" Fill="Black" StrokeThickness="5" Margin="0,0,0,0" Height="66">
<Ellipse.RenderTransform>
<RotateTransform />
</Ellipse.RenderTransform>
<Ellipse.Triggers>
<EventTrigger RoutedEvent="Ellipse.Loaded" SourceName="test">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="test"
Storyboard.TargetProperty="RenderTransform.Angle"
From="0" To="360" Duration="0:0:0.5" RepeatBehavior="1x" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Ellipse.Triggers>
</Ellipse>

Resources