Background custom button animation using WPF
Posted
by ajtp
on Stack Overflow
See other posts from Stack Overflow
or by ajtp
Published on 2010-04-23T07:20:30Z
Indexed on
2010/04/23
7:23 UTC
Read the original article
Hit count: 329
Hi,
I am using Resources dictionaries to customize my controls and apply them as themes to my WPF application so I have implemented one for the button control.
A code snippet for my custom Button.xaml is (its namespace is MyWPFApp.Themes):
<ResourceDictionary ...>
...
<LinearGradientBrush x:Key="NormalBackground"
EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="sc#1.000000, 0.250141, 0.333404, 0.884413" Offset="0"/>
<GradientStop Color="#ccffffff" Offset="1"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="OverBackground" EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#da5e69" Offset="0"/>
<GradientStop Color="#d12e27" Offset="1"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="ClickBackground" EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#d22828" Offset="1"/>
<GradientStop Color="#b00000" Offset="0"/>
</LinearGradientBrush>
...
<Style TargetType="{x:Type Button}">
...
<Setter Property="Background" Value="{StaticResource NormalBackground}"/>
...
</Style>
</ResourceDictionary>
and I apply it by doing the following from my main Application.xaml:
<Application ...>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes/Button.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
What I try to do is to change background color, for example, from White to Lime for 6 seconds by doing this from one of my pages, MyPage1.xaml, using StoryBoard:
<Page x:Class="MyWPFApp.Pages.MyPage1" ...>
<Page.Resources>
...
<Storyboard x:Key="sbBtnResetHC" Storyboard.TargetName="BtnResetHC"
Storyboard.TargetProperty="(Background).
(SolidColorBrush.Color)">
<ColorAnimation From="Pink"
To="Green"
By="Blue"
Duration="0:0:6"
RepeatBehavior="3x"
AutoReverse="True" />
</Storyboard>
...
</Page.Resources>
...
<Button x:Name="BtnResetHC"
Click="BtnResetHC_Click"
Width="90" Visibility="Collapsed" />
...
</Page>
and from code behind MyPage1.xaml.cs I start animation by doing this:
Storyboard sb = (Storyboard)FindResource("sbBtnResetHC");
sb.Begin();
when the button is visible, but it doesn't work for me.
Any ideas what's wrong?
Maybe another possibility, as I want the animation starts on button visible is to do a trigger for the button over Visibility property, Is it a better solution?
Thanks a lot!
© Stack Overflow or respective owner