I have a double animation which worked well on one control. Now I want to make it universal. How do I get current canvas of a control? For example I want to use this animation on window and it always showed from 13 to 17 while I need from 'current position' to 'current position + 4'
<BeginStoryboard x:Key="SomethingWrongAnimation">
<Storyboard Storyboard.TargetProperty="(Canvas.Left)">
<DoubleAnimation From="13" To="17" Duration="0:0:0.05"
AutoReverse="True" FillBehavior="Stop" RepeatBehavior="5x"/>
</Storyboard>
</BeginStoryboard>
Instead of From and To set the By property:
<DoubleAnimation By="4" ... />
Related
I have experienced a bit a weird behavior of the MahApps Window when I set the AllowsTransparency to true. What I am doing is to set the Opacity of the whole window to 0.4 when the mouse leaves the window. When the mouse returns to the window area, the opacity is set back to a value of 1.0.
This works fine unless I want to maximize the MahApps window. It doesn't maximize, but makes a weird blue rectangle around half of the screen, and the window remains the same size as before (The window is resizable... and without the AllowsTransparency (in code, after InitializeComponent) everything works fine).
I have uploaded a short .gif to show what I mean:
I set the opacity with following xaml:
<EventTrigger RoutedEvent="Window.MouseLeave">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity" To="0.4" Duration="0:0:0.1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="Window.MouseEnter">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0.1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
I hope anyone knows a solution for this.
Thank you in advance for your help.
Ramon
I am looking a way to animate the resizing of a window, lets say that I have a window with Height=300 and Width=300, I have 2 buttons, when I click the first button the window size must change to Height=600 and Width=600 and when I click the other button the window size must back to the original size, well I can do this simply changing the Height and Width properties, but I would like to use something like Storyboard - DoubleAnimation to give the impression that the window size is changing gradually.
I haven't used Storyboard - DoubleAnimation so if anyone can give me some tips I would appreciate it.
You cannot animate two properties in parallel Below code can help you animate the Height and Width of Window named myWindow
<Button Content="Click">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<EventTrigger.Actions>
<BeginStoryboard >
<Storyboard RepeatBehavior="Forever" AutoReverse="False">
<DoubleAnimation Storyboard.TargetName="myWindow"
Storyboard.TargetProperty = "(Window.Height)"
To="300" Duration="0:0:5"/>
<Storyboard RepeatBehavior="Forever" AutoReverse="False">
<DoubleAnimation Storyboard.TargetName="myWindow"
Storyboard.TargetProperty = "(Window.Width)"
To="300" Duration="0:0:5"/>
</Storyboard>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Button.Triggers>
</Button>
Here's my XAML, so far when someone enter any image in my Window, the animation pops up correctly.
<Style.Triggers>
<EventTrigger RoutedEvent="Image.MouseEnter">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="0:0:0.25" From="1" To="1.2" AutoReverse="True"
Storyboard.TargetProperty="RenderTransform.ScaleX"/>
<DoubleAnimation Duration="0:0:0.1" From="1" To="1.2" AutoReverse="True"
Storyboard.TargetProperty="RenderTransform.ScaleY"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
Now I'd like to create another storyboard, one that fires on Image.MouseDown, however this time the animation should change the margin of a Grid called x:Name="container".
Can I access other controls properties inside a trigger? If so, how?
I can think of 2 options here:
Create an EventTrigger for your Grid and set its SourceName property to Image name that fires MouseDown or it`s parent;
Add another EventTrigger directly to the Image and set Storyboard.TargetName for animation to Grid`s name.
I'm triying to hide/show a stackpanel when I click in a button. This is what I made so far:
<Button.Triggers>
<EventTrigger RoutedEvent="Mouse.PreviewMouseDown">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Height"
Storyboard.TargetName="PanelDeCampos"
From="{Binding ElementName=PanelDeCampos,Path=ActualHeight}"
To="0"
Duration="0:0:0.25" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
This animation works well, and it hides the panel when I click on it. But now I need to find a way to launch the reverse animation when the button is clicked again. Could I store the current state and decide what animation launch or something like this?
Thanks.
You can change your button to a ToggleButton and use the Checked and Unchecked routed events to set up your 2 storyboards:
<ToggleButton>
<ToggleButton.Triggers>
<EventTrigger RoutedEvent="Checked">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Height"
Storyboard.TargetName="PanelDeCampos"
From="{Binding ElementName=PanelDeCampos,Path=ActualHeight}"
To="0"
Duration="0:0:0.25" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="Unchecked">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Height"
Storyboard.TargetName="PanelDeCampos"
From="0"
Duration="0:0:0.25"
To="1000" /> <!-- or whatever height you want-->
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ToggleButton.Triggers>
</ToggleButton>
What about a little trick:
Together with the storyboard you have in place now, you are hidding the current button and setting another button (which looks the same) visible.
The other button has the reverse storyboard, and reverse button visibility settings.
With that you don't have to worry about a state, and you can do it only in XAML.
Other Idea would be to handle the click in the code behind, maintain a flag there, and trigger the storyboard from the code. As this is a view-only functionality I don't see a conflict with MVVM.
I have a popup with UserControl inside. I need to hide this popup and want to create it by trigger. I try the following code:
<Popup.Triggers>
<EventTrigger RoutedEvent="Popup.MouseLeave">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever">
<DoubleAnimation Storyboard.TargetName="popup"
Storyboard.TargetProperty="Popup.IsOpen"
To="False" From="True" Duration="0:0:2">
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Popup.Triggers>
<blib:TimeZones></blib:TimeZones>
</Popup>
but it does not work
Failed to assign to property
'System.Windows.EventTrigger.RoutedEvent'. [Line: 55 Position: 47]
How to make it correctly?
Triggers in Silverlight don't work in the same way as WPF triggers do. You need to use the interactivity library. Check out this article: http://www.silverlightshow.net/items/Behaviors-and-Triggers-in-Silverlight-3.aspx