I have an Image and a Popup. When clicked on the Image popup should open.
I started like that and now I stuck.
<Image x:Name="LockImage" Source="/Lock.png">
<Image.Triggers>
<EventTrigger RoutedEvent="MouseDown">
// ?????? WHAT's here?
</EventTrigger>
</Image.Triggers>
</Image>
<Popup x:Name="LockPopup" PlacementTarget="{Binding ElementName=LockImage}">
<TextBlock Text="This is a popup" />
</Popup>
UPD... Ooops, actually I forgot... I'd like the popup to be shown not immediately but rather after a second or two. If it was just a click, It would be something else... (default action)
Here is the solution of what you want to do. Delay time can be set at Storyboard definitions. Insert this code into new wpf app project Window.xaml file.
<Window.Resources>
<Storyboard x:Key="ShowPopup">
<BooleanAnimationUsingKeyFrames Storyboard.TargetName="LockPopup" Storyboard.TargetProperty="(Popup.IsOpen)">
<DiscreteBooleanKeyFrame KeyTime="00:00:00.5" Value="True" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="HidePopup" Storyboard.TargetName="LockPopup" Storyboard.TargetProperty="(Popup.IsOpen)">
<BooleanAnimationUsingKeyFrames>
<DiscreteBooleanKeyFrame KeyTime="00:00:00.5" Value="False" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
<Grid x:Name="grid" ShowGridLines="True">
<Image x:Name="LockImage" Stretch="None" >
<Image.Source>
<DrawingImage>
<DrawingImage.Drawing>
<GeometryDrawing Brush="Black">
<GeometryDrawing.Geometry>
<EllipseGeometry RadiusX="10" RadiusY="10"/>
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingImage.Drawing>
</DrawingImage>
</Image.Source>
<Image.Triggers>
<EventTrigger RoutedEvent="Image.MouseLeftButtonDown">
<BeginStoryboard Storyboard="{StaticResource ShowPopup}"/>
</EventTrigger>
<EventTrigger RoutedEvent="Image.MouseLeave">
<BeginStoryboard Storyboard="{StaticResource HidePopup}"/>
</EventTrigger>
</Image.Triggers>
</Image>
<Popup x:Name="LockPopup" PlacementTarget="{Binding ElementName=LockImage}" DataContext="{Binding}" Placement="Bottom">
<TextBlock Text="This is a popup" Background="White" Foreground="Black" />
</Popup>
</Grid>
Related
based on this answer i have two questions:
Whats the name of the marked icon?
How can I make it to raotate to the other direction, i.e to spin like:
Itried to add FlowDirection="RightToLeft" like:
<fa:ImageAwesome FlowDirection="RightToLeft" SpinDuration="6" Icon="Cog" Width="200" Height="200" Foreground="White" Spin="True" />
but it still rotate to same direction
this should do the trick for you:
<Viewbox Height="20">
<Path Fill="black" Data="M28,2.202v4.059C37.053,7.706,44,15.547,44,25c0,10.477-8.523,19-19,19S6,35.477,6,25c0-9.442,6.93-17.275,15.966-18.734 V2.206C10.713,3.696,2,13.347,2,25c0,12.682,10.317,23,23,23s23-10.318,23-23C48,13.335,39.269,3.677,28,2.202z" RenderTransformOrigin="0.5,0.5" >
<Path.RenderTransform>
<RotateTransform/>
</Path.RenderTransform>
<Path.Style>
<Style>
<Style.Triggers>
<Trigger Property="Image.IsEnabled" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetProperty="RenderTransform.Angle"
From="0"
To="360"
Duration="0:0:1"
RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
</Style.Triggers>
</Style>
</Path.Style>
</Path>
</Viewbox>
I am using MVVM so I need to bind a rectangle shape a property which will eventually animate it.
I've found few solutions and tried to replicate their approach but the storybroad doesnt kick in
I am including my source code
<Rectangle x:Name="RectangleCompleted" HorizontalAlignment="Left" Height="50" Margin="440,178,0,0" VerticalAlignment="Top" Width="100" Stroke="#FF0BB3AE" StrokeThickness="5" RadiusX="8" RadiusY="7" >
<i:Interaction.Triggers>
<ei:DataTrigger Binding="{Binding Path=Animation}" Value="ON" Comparison="Equal">
<ei:ControlStoryboardAction
Storyboard="{StaticResource MyAnimation}" ControlStoryboardOption="Play"/>
</ei:DataTrigger>
</i:Interaction.Triggers>
<Rectangle.Effect>
<DropShadowEffect RenderingBias="Quality" ShadowDepth="3" Color="#FFD2D6D8"/>
</Rectangle.Effect>
<Rectangle.Fill>
<SolidColorBrush Color="#FFE7F4F6"/>
</Rectangle.Fill>
</Rectangle>
<Window.Resources>
<Storyboard x:Key="MyAnimation">
<DoubleAnimation
Storyboard.TargetName="RectangleCompleted"
Storyboard.TargetProperty="Opacity"
From="0"
To="1"
RepeatBehavior="Forever"
AutoReverse="True"/>
</Storyboard>
</Window.Resources>
Is your Animation property a bool or string? If it's a bool you need the value in your trigger check for True or False. If it's a string, then you should be fine with what you got. Provided your implementation of INPC and the Animation property is right this should work fine.
I've put together an example with the code you've posted and you should see it work fine. Download Link
I've made two small change. I've added a Button to toggle the animation to either start or stop and put the entire thing in a StackPanel. Secondly I made the Animation property a bool so just had the Triggers to check for "True" or "False" than the "ON" value you were checking for.
<StackPanel x:Name="LayoutRoot">
<Rectangle x:Name="RectangleCompleted"
Width="100"
Height="50"
RadiusX="8"
RadiusY="7"
Stroke="#FF0BB3AE"
StrokeThickness="5">
<Rectangle.Resources>
<Storyboard x:Key="MyAnimation">
<DoubleAnimation AutoReverse="True"
From="0"
RepeatBehavior="Forever"
Storyboard.TargetName="RectangleCompleted"
Storyboard.TargetProperty="Opacity"
To="1" />
</Storyboard>
</Rectangle.Resources>
<i:Interaction.Triggers>
<ei:DataTrigger Binding="{Binding Path=Animation}"
Comparison="Equal"
Value="True">
<ei:ControlStoryboardAction ControlStoryboardOption="Play"
Storyboard="{StaticResource MyAnimation}" />
</ei:DataTrigger>
<ei:DataTrigger Binding="{Binding Path=Animation}"
Comparison="Equal"
Value="False">
<ei:ControlStoryboardAction ControlStoryboardOption="Stop"
Storyboard="{StaticResource MyAnimation}" />
</ei:DataTrigger>
</i:Interaction.Triggers>
<Rectangle.Effect>
<DropShadowEffect RenderingBias="Quality"
ShadowDepth="3"
Color="#FFD2D6D8" />
</Rectangle.Effect>
<Rectangle.Fill>
<SolidColorBrush Color="#FFE7F4F6" />
</Rectangle.Fill>
</Rectangle>
<Button Command="{Binding ToggleCommand}"
Content="Toggle Animation" />
</StackPanel>
and all ToggleCommand does is:
ToggleCommand = new RelayCommand(() => Animation = !Animation);
Update:
If you just want to trigger your animation when the window loads, all you need is:
<Window.Resources>
<Storyboard x:Key="MyAnimation">
<DoubleAnimation AutoReverse="True"
From="0"
RepeatBehavior="Forever"
Storyboard.TargetName="RectangleCompleted"
Storyboard.TargetProperty="Opacity"
To="1" />
</Storyboard>
</Window.Resources>
<Window.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard Storyboard="{StaticResource MyAnimation}" />
</EventTrigger>
</Window.Triggers>
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 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>
I am trying to increase the size of the image by 20. So I am using ScaleTransform as shown below.. but the following code doesn't do any scale Tranform.. Any help would be appreciated...
<Grid>
<Canvas>
<Canvas Height="50" Width="50" Canvas.Top="10" Canvas.Left="100"
Visibility="Visible">
<Image Name="Img" Source="Help.PNG" Canvas.Left="0" Canvas.Top="0">
</Image>
</Canvas>
<Button Canvas.Left="100" Canvas.Top="100" Height="42.5" Name="button3"
Width="100" Visibility="Visible">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard Name="MoveBox">
<DoubleAnimation Storyboard.TargetName="Img"
Storyboard.TargetProperty="(Image.RenderTransform).(ScaleTransform.ScaleX)"
From="1" To="20" BeginTime="0:0:3.75" Duration="0:0:1.25" />
<DoubleAnimation Storyboard.TargetName="Img"
Storyboard.TargetProperty="(Image.RenderTransform).(ScaleTransform.ScaleY)"
From="1" To="20" BeginTime="0:0:3.75" Duration="0:0:1.25" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
</Canvas>
</Grid>
Have you tried setting a <RenderTransform> on the image? Something like this:
<Image Name="Img" Source="Help.PNG" Canvas.Left="0" Canvas.Top="0">
<Image.RenderTransform>
<ScaleTransform x:Name="scale" ScaleX="1" ScaleY="1"
CenterX="0.5" CenterY="0.5" />
</Image.RenderTransform>
</Image>
This initialises the RenderTransform so that you can refer to it from elsewhere.
I've had to do this with Silverlight.