WPF : Animating user Control on load - wpf

I am developing an application in which I have a user control inside a window. I want to perform some growing animation on the size of that control when it gets attached to the window.
Can anyone help me with it ??

In your styling:
<YourControl.Triggers>
<EventTrigger RoutedEvent="YourControl.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Width" From="0" To="150" Duration="0:0:5" />
<DoubleAnimation Storyboard.TargetProperty="Height" From="0" To="100" Duration="0:0:5" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</YourControl.Triggers>

Related

RoutedEvent TextBox.LostFocus not working in my WPF application

I am working with the WPF animations using Storyboards. What I want is to fade out all the content of the page as soon as my textbox gets focus, and to fade in everything back as soon as the focus is removed from the textbox. To do that, I have the following XAML with no code behind.
<Page.Triggers>
<EventTrigger RoutedEvent="TextBox.GotFocus">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="Grid1"
Storyboard.TargetProperty="Opacity"
From="1" To="0.1" Duration="0:0:0.2"
AutoReverse="False" >
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="TextBox.LostFocus">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="Grid1"
Storyboard.TargetProperty="Opacity"
From="0.1" To="1" Duration="0:0:0.2"
AutoReverse="False">
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
To move focus out of textbox, I have the following button:
<Button
Name="SearchButton"
Height="30" Width="30"
Grid.Column="1"
Focusable="True"
IsHitTestVisible="True"
Style="{StaticResource SearchButton}"
Padding="3,3,3,3"
Margin="3,3,3,3" Click="Button_Click"/>
When I run the app, clicking on the textbox makes the fade out work fine. But when I click on the button, the fade in does not kick in.
Can anyone please give me some insights?
You should place your triggers in the TextBox directly, not on the Page level:
<TextBox>
<TextBox.Triggers>
<EventTrigger RoutedEvent="TextBox.GotFocus">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="Grid1"
Storyboard.TargetProperty="Opacity"
From="1" To="0.1" Duration="0:0:0.2"
FillBehavior="HoldEnd">
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="TextBox.LostFocus">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="Grid1"
Storyboard.TargetProperty="Opacity"
From="0.1" To="1" Duration="0:0:0.2"
FillBehavior="Stop">
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBox.Triggers>
</TextBox>
Otherwise, the storyboards will be triggered for each and every GotFocus and LostFocus routed event of each UIElement on the Page because of these events' bubbling strategy.

How to set time between single Animation repeats in WPF?

I have a DoubleAnimation for a waiting control and it repeats forever. But I want to set time between every repeats. How can I do that?
My animation is:
<DoubleAnimation Storyboard.TargetName="rect1"
Storyboard.TargetProperty="Height"
To="10" BeginTime="0:0:0" Duration="0:0:0.3"
AutoReverse="True" RepeatBehavior="Forever"/>
Set a Duration on the enclosing Storyboard:
<Storyboard AutoReverse="True" RepeatBehavior="Forever" Duration="0:0:1">
<DoubleAnimation Storyboard.TargetName="rect1"
Storyboard.TargetProperty="Height"
To="10" Duration="0:0:0.3"/>
</Storyboard>

Make WPF control disappear when lost focus

I have a StackPanel that is being used as a container for other menuing controls. I want the StackPanel to disappear when somewhere else on the UI is clicked (similar to typical menus/context menus). I'm struggling with how to do this. Any suggestions? I've tried event triggers in the style like below but it doesn't seem to work correctly.
<StackPanel.Style>
<Style TargetType="{x:Type StackPanel}">
<Style.Triggers>
<EventTrigger RoutedEvent="LostMouseCapture" >
<BeginStoryboard>
<Storyboard>
<DoubleAnimation From="1" To="0" Duration="0:0:0.1"
Storyboard.TargetProperty="Opacity"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
Set the stackPanel's trigger like this:
<EventTrigger RoutedEvent="MouseEnter" >
<BeginStoryboard>
<Storyboard>
<DoubleAnimation To="1" Duration="0:0:0.1"
Storyboard.TargetProperty="Opacity"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
and add this tag to Window (as well as other controls you want to when clicked, hide the stack panel):
<Window.Triggers>
<EventTrigger RoutedEvent="MouseDown">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation To="0" Duration="0:0:0.1"
Storyboard.TargetName="disappearingStackPanel"
Storyboard.TargetProperty="Opacity"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Window.Triggers>
You can also set IsHitTestVisible=False on controls that don't have mouse interaction so you don't have to add the trigger to them.

xaml button focus with button mouse over

I created a button style for my project. I want the button to flash when the user moves the cursor over it. I do it with this code:
<Trigger Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="Background.Color" Duration="0:0:0.2" From="#808000" To="#ffaec9" RepeatBehavior="Forever" AutoReverse="True"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
I also want then user focus button it to also flash. But if I create similar code for IsFocused property will not work as I want. Can i use the property concatenation in Property attribute of tag trigger? Can i create sequence of triggers (trigger1 work if it worked before triпger2 and will not work if worked trigger3)?
use this state, it will work on your button
<VisualState x:Name="MouseOver">
<Storyboard>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="(UIElement.Effect).(DropShadowEffect.Opacity)" Storyboard.TargetName="Background" d:IsOptimized="True"/>
</Storyboard>
</VisualState>

WPF Window fadeIn does not work

I have the following code:
<Window.Background>
<SolidColorBrush Opacity="0.7" Color="White" x:Name="BackgroundBrush"></SolidColorBrush>
</Window.Background>
<Window.Triggers>
<EventTrigger RoutedEvent="Loaded">
<EventTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:5" Storyboard.TargetName="BackgroundBrush" From="0.7">
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger.EnterActions>
</EventTrigger>
</Window.Triggers>
But nothing happened when window is shown. Why?
You have to animate the opacity of the window itself, rather than the background.
You need to set AllowsTransparency to true, which also necessitates the WindowStyle to be set to None. (You need to create your own standard window buttons)
In addition to what H.B. said, you need to add your BeginStoryboard to the EventTrigger.Actions collection, not the EnterActions collection. So this works:
<Window.Background>
<SolidColorBrush Opacity="0.7" Color="White" x:Name="BackgroundBrush"></SolidColorBrush>
</Window.Background>
<Window.Triggers>
<EventTrigger RoutedEvent="Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:5" Storyboard.TargetName="BackgroundBrush" From="0.7">
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Window.Triggers>

Resources