I'm new to WPF and have a probably stupid question.
I'm trying to animate 4 buttons with the same animation (rotate 360 degrees) when one of them is clicked and only this one gets animated.
Here is what I have so far:
<Window.Resources>
<Storyboard x:Key="Storyboard" BeginTime="00:00:00" Duration="00:00:10">
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="rotButton" Storyboard.TargetProperty="(RotateTransform.Angle)">
<SplineDoubleKeyFrame KeyTime="0:0:00.0" Value="0.0" />
<SplineDoubleKeyFrame KeyTime="0:0:01.0" Value="360.0" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
And rotButton is defined in the first button here:
<Button Click="Button_Click">
<StackPanel>
<Image Source="open.png" Height="46" Width="48" />
</StackPanel>
<Button.RenderTransform>
<TransformGroup>
<RotateTransform x:Name="rotButton" Angle="0" CenterX="25" CenterY="25" />
<ScaleTransform x:Name="scaButton" ScaleX="1" ScaleY="1" CenterX="50" CenterY="25" />
</TransformGroup>
</Button.RenderTransform>
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard Storyboard="{StaticResource Storyboard}" />
</EventTrigger>
</Button.Triggers>
</Button>
How can I use this code for all other buttons and have "common" Button.RenderTransform for every button? There should be a lot more smarter way of creating 3 more storyboards and using rotButton1, rotButton2, etc for each button.
I hope it makes sense and point me in the right direction :)
Thanks
If you create a style for your button, you can use a setter to set the RenderTransform for each instance of button that uses that Style. Also, styles can have triggers.
The trick is the right path syntax http://blogs.charteris.com/blogs/patl-closed/archive/2007/03/20/Complex-PropertyPath-syntax.aspx
<Window.Resources>
<TransformGroup x:Key="transformGroup">
<RotateTransform Angle="0" CenterX="25" CenterY="25" />
<ScaleTransform ScaleX="1" ScaleY="1" CenterX="50" CenterY="25" />
</TransformGroup>
<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
<Setter Property="RenderTransform" Value="{StaticResource transformGroup}"/>
<Style.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard BeginTime="00:00:00" Duration="00:00:10">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Button.RenderTransform).(TransformGroup.Children)[0].(RotateTransform.Angle)">
<SplineDoubleKeyFrame KeyTime="0:0:00.0" Value="0.0" />
<SplineDoubleKeyFrame KeyTime="0:0:01.0" Value="360.0" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<StackPanel>
<Button Style="{StaticResource MyButtonStyle}"/>
<Button Style="{StaticResource MyButtonStyle}"/>
</StackPanel>
</Grid>
Related
I have a simple image on my form. What I would like to do is when I hover the image it starts a storyboard which basically does a 360 loop on itself.
Here's the storyboard, it's called TurnLogo:
<Storyboard x:Key="TurnLogo">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" Storyboard.TargetName="image">
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="360"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
Here's my image :
<Image x:Name="image" HorizontalAlignment="Left" Height="64" VerticalAlignment="Top" Width="64" Source="Images/Logo/Logomakr_3lb9fd.png" Margin="7,7,0,0" RenderTransformOrigin="0.5,0.5">
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Image.RenderTransform>
<Image.Style>
<Style>
<Style.Triggers>
<EventTrigger RoutedEvent="Control.MouseEnter">
<BeginStoryboard Storyboard="{StaticResource TurnLogo}"/>
</EventTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
When I hover the image it fails.. Why ?
Remove the Storyboard.TargetName:
<Storyboard x:Key="TurnLogo">
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.Angle"
By="360" Duration="0:0:1"/>
</Storyboard>
and simplify the RenderTransform:
<Image ... RenderTransformOrigin="0.5,0.5">
<Image.RenderTransform>
<RotateTransform/>
</Image.RenderTransform>
<Image.Style>
<Style TargetType="Image">
<Style.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard Storyboard="{StaticResource TurnLogo}"/>
</EventTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
I have border, that I want to animate by clicking button (name = "button1"). Button is outside. My code throws an exception. Whats wrong? Thanks
<Border Name="brdClasses" Background="#FF2c3e50">
<Border.RenderTransform>
<ScaleTransform x:Name="MyAnimatedScaleTransform"
ScaleX="1" ScaleY="1" />
</Border.RenderTransform>
<Border.Triggers>
<EventTrigger SourceName="button1" RoutedEvent="Button.Click">
<EventTrigger.Actions>
<BeginStoryboard Name="MyBeginStoryboard">
<Storyboard >
<DoubleAnimation
Storyboard.TargetName="MyAnimatedScaleTransform"
Storyboard.TargetProperty="(ScaleTransform.ScaleX)"
To="3.0" Duration="0:0:10" AutoReverse="True" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Border.Triggers>
</Border>
I Dont know why your code doent works... But I did a simple example in a specific way.. if it helps make use of that..
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Border Name="brdClasses" Grid.Row="1" Background="#FF2c3e50" Height="100" Width="150">
<Border.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Border.RenderTransform>
</Border>
<Button Content="Button1" x:Name="button1">
<Button.Triggers>
<EventTrigger RoutedEvent="ButtonBase.Click" SourceName="button1">
<BeginStoryboard>
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="brdClasses">
<EasingDoubleKeyFrame KeyTime="0" Value="3"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
</Grid>
I'm trying to change the height of a WPF grid from 0 to Auto using Storyboard. I know I can't do it without tricking, so I tried to change the MaxHeight Value, using Databinding in the Storyboard. But I can't make it works (height is always equals to 0). Here is the code I use :
The two visualstates :
<VisualState x:Name="SelectionMode">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="CurrentToolGrid" Storyboard.TargetProperty="(FrameworkElement.MaxHeight)">
<EasingDoubleKeyFrame KeyTime="0" Value="0" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="EditionMode">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="CurrentToolGrid" Storyboard.TargetProperty="(FrameworkElement.MaxHeight)">
<EasingDoubleKeyFrame KeyTime="0" Value="{Binding ElementName=CurrentToolGrid, Path=ActualHeight}" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
Here is my grid definition :
<Grid x:Name="CurrentToolGrid" Background="#FFDEDEDE" Grid.Row="1" Height="Auto">
[... some controls that extends the grid's height ...]
</Grid>
Why the height of the CurrentToolGrid is always 0 ?
Thanks for helping me :)
Please refer to following code, I use TranslateTransform instead of MaxWidth, it make scense for Slide scenario.The key to impl this is StoryBoard should define in resource, then storyboard can find the specify control which binding in doubleanimation.
<Window x:Class="SlideUpWPFTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
x:Name="Window"
Title="MainWindow"
Width="640"
Height="480">
<Window.Resources>
<Storyboard x:Key="sb1">
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="grdContent" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
<EasingDoubleKeyFrame KeyTime="0" Value="{Binding ElementName=grdContent, Path=ActualWidth}" />
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="0" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="sb2">
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="grdContent" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
<EasingDoubleKeyFrame KeyTime="0" Value="0" />
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="{Binding ElementName=grdContent, Path=ActualWidth}" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
<Grid x:Name="LayoutRoot" RenderTransformOrigin="0.5,0.5">
<Grid.RowDefinitions>
<RowDefinition Height="35" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform />
<SkewTransform />
<RotateTransform />
<TranslateTransform />
</TransformGroup>
</Grid.RenderTransform>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="VisualStateGroup">
<VisualState x:Name="EditMode" Storyboard="{StaticResource sb1}" />
<VisualState x:Name="SelectionMode" Storyboard="{StaticResource sb2}" />
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<StackPanel HorizontalAlignment="Right" Orientation="Horizontal">
<Button Content="Enter Edit Mode">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:GoToStateAction StateName="EditMode" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<Button Content="Enter Selected Mode">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:GoToStateAction StateName="SelectionMode" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</StackPanel>
<Grid x:Name="grdContent"
Grid.Row="1"
RenderTransformOrigin="0.5,0.5">
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform />
<SkewTransform />
<RotateTransform />
<TranslateTransform />
</TransformGroup>
</Grid.RenderTransform>
<!-- some content -->
<Rectangle Height="65" Fill="LightBlue" />
</Grid>
</Grid>
</Window>
I want to move the images one by one like slides. i am using the following code to move one image. How to apply this animation to all the images in the image folder.
Code:
<Image Name="img" Width="50" Height="25" Grid.Row="3" HorizontalAlignment="Left" Source="btn_audio_stop.jpg">
<Image.Triggers>
<EventTrigger RoutedEvent="Image.Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="Trans" Storyboard.TargetProperty="Y" Duration="0:0:25">
<LinearDoubleKeyFrame Value="350" KeyTime="0:0:25" />
<!--<LinearDoubleKeyFrame Value="50" KeyTime="0:0:5" />
<LinearDoubleKeyFrame Value="200" KeyTime="0:0:3" />-->
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Image.Triggers>
<Image.RenderTransform>
<TranslateTransform x:Name="Trans" X="0" Y="0" />
</Image.RenderTransform>
</Image>
See following articles
http://www.c-sharpcorner.com/UploadFile/prvn_131971/ImageSlideshowWPF11162008224421PM/ImageSlideshowWPF.aspx
http://www.c-sharpcorner.com/UploadFile/dpatra/569/
I don't know how to link successfully a StoryBoard to a TranslateTransform that is part of a ContentControl. I always get the following error when I try to run my StoryBoard:
'RenderTransform' property does not point to a DependencyObject in path '(Children).[0].(Content).(0).(1)'.
I guess I don't know how to define a TargetProperty properly! I tried many different paths but always failed. Here is a simplified version of my code:
The DataTemplate:
<DataTemplate x:Key="bdAnswer">
<Border>
<Border.RenderTransform>
<TranslateTransform X="0" Y="0"/>
</Border.RenderTransform>
</Border>
</DataTemplate>
The Canvas where the DataTemplate is used:
<Canvas x:Name="cnvGame">
<ContentControl ContentTemplate="{StaticResource bdAnswer}" />
<ContentControl ContentTemplate="{StaticResource bdAnswer}" />
</Canvas>
And my StoryBoard:
<Storyboard x:Key="sbGame">
<DoubleAnimationUsingKeyFrames
BeginTime="00:00:00"
Storyboard.TargetName="cnvGame"
Storyboard.TargetProperty="(Children)[0].(Content).(UIElement.RenderTransform).(TranslateTransform.Y)" />
</Storyboard>
Many thanks!
That all depends on how you intend to trigger that Storyboard. You were kind of vague, so it might not fit your situation. Everything is contained within the DataTrigger, everything is within the same scope and ascertaining that translate Y property is easy this way.
<DataTemplate x:Key="bdAnswer">
<DataTemplate.Resources>
<Storyboard x:Key="Storyboard1">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="border" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)">
<SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</DataTemplate.Resources>
<Border x:Name="border" RenderTransformOrigin="0.5,0.5">
<Border.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform Y="10"/>
</TransformGroup>
</Border.RenderTransform>
<TextBlock Text="A Bar of Foo"/>
</Border>
<DataTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard x:Name="Storyboard1_BeginStoryboard" Storyboard="{StaticResource Storyboard1}"/>
</Trigger.EnterActions>
<Trigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="Storyboard1_BeginStoryboard"/>
</Trigger.ExitActions>
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
Here is an example of something I have used
<DataTemplate x:Key="PM_ORDERSTemplate">
<DataTemplate.Resources>
<Storyboard x:Key="OnChecked1">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="image" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
<SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="OnUnchecked1">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="image" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/>
<SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="OnLoaded1">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="image">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</DataTemplate.Resources>
<RadioButton x:Name="radioButton" GroupName="OrderSelect" BorderThickness="1,1,1,1" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Style="{DynamicResource RadioButtonOrderPicker}" Checked="RadioButton_Checked" Template="{DynamicResource RadioButtonControlTemplate1}" Margin="5,5,5,0" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="{x:Null}">
<Border x:Name="bOrderPicker" BorderThickness="5,5,5,5" BorderBrush="{DynamicResource DNP-MediaPlayerBorderColor}" CornerRadius="10,10,10,10" BitmapEffect="{DynamicResource DNP-OrderPickerShadow}" MinHeight="45" Padding="5" d:LayoutOverrides="Width, Height, GridBox" >
<Border.Background>
<LinearGradientBrush EndPoint="125000,1704.038" StartPoint="125000,0" MappingMode="Absolute" SpreadMethod="Pad">
<GradientStop Color="#19FFFFFF" Offset="0"/>
<GradientStop Color="#34FFFFFF" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image x:Name="image" Width="40" Height="40" Source="Res/Branding/icon_check.png" Margin="2"/>
<Grid Margin="0,0,0,0" d:LayoutOverrides="Width" Grid.Column="1" >
<Label Content="{Binding ORDER_ID.Value" />
</Grid>
</Grid>
</Border>
</RadioButton>
<DataTemplate.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded" SourceName="radioButton">
<BeginStoryboard x:Name="OnUnchecked1_BeginStoryboard1" Storyboard="{StaticResource OnLoaded1}"/>
</EventTrigger>
<EventTrigger RoutedEvent="ToggleButton.Checked" SourceName="radioButton">
<BeginStoryboard Storyboard="{StaticResource OnChecked1}"/>
</EventTrigger>
<EventTrigger RoutedEvent="ToggleButton.Unchecked" SourceName="radioButton">
<BeginStoryboard x:Name="OnUnchecked1_BeginStoryboard" Storyboard="{StaticResource OnUnchecked1}"/>
</EventTrigger>
</DataTemplate.Triggers>
</DataTemplate>