How to animate button at startup - c#

I am doing a startup button which prompts the user to click it at the start.
I would like to animate my button at startup, with the animation of the button mouseover and mouseout continuously so that it looks like its blinking. And then stop animating it when it is clicked.
Any idea how to do so?

Your main storyboard would be instigated by the Loaded event of the button. Then you would have another event trigger on the Click event which removes the storyboard created in the Loaded event.
For example:
<Button>
<Button.Background>
<SolidColorBrush Color="White"/>
</Button.Background>
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Loaded">
<BeginStoryboard Name="MainStoryboard">
<Storyboard Storyboard.TargetProperty="Background.Color" Duration="00:00:02" RepeatBehavior="Forever">
<ColorAnimation To="Black" AutoReverse="True"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="Button.Click">
<RemoveStoryboard BeginStoryboardName="MainStoryboard"/>
</EventTrigger>
</Button.Triggers>
Test
</Button>

Related

WPF EventTriggers.EnterActions not working

WPF EventTriggers.EnterActions not working.
Ust two similar Storyboard in Trigger.Actions - work, but in Trigger.EnterActions - no.
Trigger on checkbox 2 works, but trigger on checkbox 1 not firing.
WPF EventTriggers.EnterActions not working.
Ust two similar Storyboard in Trigger.Actions - work, but in Trigger.EnterActions - no.
<StackPanel>
<CheckBox Content="checkbox 1">
<CheckBox.Triggers>
<EventTrigger RoutedEvent="CheckBox.Checked">
<EventTrigger.EnterActions>
<BeginStoryboard Name="enter">
<Storyboard>
<DoubleAnimation From="1"
To="0.5"
Duration="0:0:1"
Storyboard.TargetProperty="Opacity"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.EnterActions>
<EventTrigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="enter"/>
</EventTrigger.ExitActions>
</EventTrigger>
</CheckBox.Triggers>
</CheckBox>
<CheckBox Content="checkbox 2">
<CheckBox.Triggers>
<EventTrigger RoutedEvent="CheckBox.Checked">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard >
<DoubleAnimation From="1"
To="0.5"
Duration="0:0:1"
Storyboard.TargetProperty="Opacity"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</CheckBox.Triggers>
</CheckBox>
</StackPanel>
WPF EventTriggers.EnterActions not working. Ust two similar Storyboard in Trigger.Actions - work, but in Trigger.EnterActions - no.
See documentation TriggerBase.EnterActions:
Gets a collection of TriggerAction objects to apply when the trigger object becomes active. This property does not apply to the EventTrigger class.

Show the GridVisibility based on button Command

I have a Grid which should be collapsed by default it is in A xaml file.
Another Button in Grid in another xaml file.When clcik button Grid should be enabled.
How to achieve this I have Tried this can you please help
<Grid Visibility={Binding Visibilityproperty}/>
<Button Content="A" Command={Binding VisibilityCommand"}/>
In button command i have written the logic to enable the visibilityproperty to visible
But grid is not at all visible if i do like this
Better solution in MVVM pattern is welcomed
<Button Content="Button!">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.Target="{x:Reference dataGrid}"
Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0:0:0"
Value="{x:Static Visibility.Visible}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
{x:Reference dataGrid} references a DataGrid with the name dataGrid, alternatively you could just use Storyboard.TargetName. You would normally use the Storyboard.Target property if you do binding or references to resources.

Stop animation outside of scope

I have a complicated animation, which does some heavy changes to a view (changes visibility, opacity, background brushes, etc. of some controls) and I'd like to revert what this animation did. Stop/Remove storyboard "should" do that.
However, there is a problem:
The animation runs when one button is clicked, but stopped when another is clicked. And with this approach I am getting the following error.
System.Windows.Media.Animation Warning: 6 : Unable to perform action because the specified Storyboard was never applied to this object for interactive control.; Action='Remove' ......
Here is how I am doing it:
<!-- button which start animation -->
<Button ...>
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<EventTrigger.Actions>
<BeginStoryboard x:Name="storyboardUserClick">
<Storyboard>
<ObjectAnimationUsingKeyFrames ...
<!-- button which should revert what animation did --->
<Button ...>
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<EventTrigger.Actions>
<RemoveStoryboard BeginStoryboardName="storyboardUserClick" ...
Is there a simple way, preferably without code behind (well, attached property may be an option in worst case) to achieve that? I have feeling it is something very simple...
I found the answer here.
The idea is to move animation (I say animation, but I mean Storyboard and even more specifically BeginStoryboard) into a scope accessible by RemoveStoryboard. Something like
<Grid>
<Button x:Name="buttonStart" ...>
<Button x:Name="buttonStop" ...>
<Grid.Triggers>
<EventTrigger SourceName="buttonStart" RoutedEvent="Button.Click">
<BeginStoryboard x:Name="storyboardUserClick">
<Storyboard>
<ObjectAnimationUsingKeyFrames ...
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger SourceName="buttonStop" RoutedEvent="Button.Click">
<RemoveStoryboard BeginStoryboardName="storyboardUserClick" />
</EventTrigger>
</Grid.Triggers>
</Grid>

Stopping a double animation

In my UserControl.Resources i have defined a Storyboard which triggers a DoubleAnimation to set the Opactiy.
<Storyboard AutoReverse="True" x:Key="BlinkingStoryBoard">
<DoubleAnimation Storyboard.TargetProperty="Opacity" To="0.1" Duration="0:0:2" RepeatBehavior="Forever"/>
</Storyboard>
This Storyboard is applied to a grid when the UserControl is loaded:
<Grid Name="ImagePlaceHolder">
<Rectangle Fill="#D52B1E" Width="75" Height="75"/>
<Image Source="pack://siteoforigin:,,,/Resources/Images/cross.png" Width="75" Height="75" RenderTransformOrigin="0.5, 0.5" x:Name="Cross"/>
<Grid.Triggers>
<EventTrigger RoutedEvent="UserControl.Loaded">
<BeginStoryboard Storyboard="{StaticResource BlinkingStoryBoard}"/>
</EventTrigger>
</Grid.Triggers>
</Grid>
From my code-behind I want to stop this animation, but I cant stop it!
((Storyboard)uc.FindResource("BlinkingStoryBoard")).Stop();
Any ideas?
Have a look at this reference.
The main idea is that you need to call the storyboard Stop method passing the container of the storyboard.
You should try replacing the stop call with this:
((Storyboard)uc.FindResource("BlinkingStoryBoard")).Stop(ImagePlaceHolder);
Also from the reference, it shows that you can name your BeginStoryboard, for example:
<BeginStoryboard Name="MyStoryboardStarter" Storyboard="{StaticResource BlinkingStoryBoard}"/>
Now in the code behind you can find your storyboard more easily and stop it like this:
MyStoryboardStarter.Storyboard.Stop(ImagePlaceHolder);
Hope this helps.

How to animate ListBox Items on MouseEnter and MouseLeave events using C#/WPF?

I can't capture/trigger OnMouseEnter or OnMouseLeave events through C# code for list items. To be clear, I don't need an OnSelectedItem event.
What I want to do is to be able to handle the OnMouseEnter and OnMouseLeave events for ListBoxItem which will start the DoubleAnimation for that ListBoxItem - I want to enlarge its font on MouseEnter and restore to original size on MouseLeave.
Any ideas? Thanks.
Something like this (as part of the ListBox's DataTemplate):
<DataTemplate.Triggers>
<EventTrigger
SourceName="BorderControl"
RoutedEvent="TextBlock.MouseEnter">
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="BorderControl"
Storyboard.TargetProperty="Background.Color"
To="DarkRed" Duration="00:00:00.2" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger
SourceName="BorderControl"
RoutedEvent="TextBlock.MouseLeave">
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="BorderControl"
Storyboard.TargetProperty="Background.Color"
To="WhiteSmoke" Duration="00:00:00.2" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</DataTemplate.Triggers>
via http://www.dotnet-blog.com/index.php/2009/01/29/how-to-style-and-animate-a-wpf-listbox/

Categories