Wpf Animate Background Color

Wpf animate background color

I would use an EventTrigger with a ColorAnimation.

In this example a Button Brackground goes green on a MouseLeave event. This code is hopefully similar to what you may need.

<Button Content="Button" Height="75" HorizontalAlignment="Left" Margin="27,12,0,0" Name="btnImgBrush" VerticalAlignment="Top" Width="160" Background="LightGray">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.MouseLeave">
<BeginStoryboard>
<Storyboard>
<ColorAnimation To="Green"
Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)"
FillBehavior="Stop"
Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>

Animate Grid Background color

First of all, StyleTriggers in general dont support TargetNames.

AFAIK only TemplateTriggers do support them.

Your DataTrigger should look like this.

<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="Background.Color" To="LimeGreen"
FillBehavior="Stop" AutoReverse="True" Duration="0:0:5" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>

Notice the Storyboard.TargetProperty="Background.Color". Also your TimeSpan was too high. Another thing is setting the AutoReverse to true.

EDIT

To get this Trigger to work, in the Grid's Style there has an initial Background to be set.

<Style TargetType="Grid">
<Setter Property="Background" Value="Red"/>

WPF: How to animate color change?

Solved!

private void Window_Loaded(object sender, RoutedEventArgs e)
{
SolidColorBrush rootElementBrush;
ColorAnimation animation;

rootElementBrush = this.FindResource("RootElementBrush") as SolidColorBrush;

// Animate the brush
animation = new ColorAnimation();
animation.To = Colors.Green;
animation.Duration = new Duration(TimeSpan.FromSeconds(5));
rootElementBrush.BeginAnimation(SolidColorBrush.ColorProperty, animation);
}

Here's an explanation:

My initial mistake was that I wanted to change the Grid.BackgroundProperty by assigning colors to it, but it accepts brushes instead... apples and oranges! So, I created a SolidColorBrush static resource and named it rootElementBrush. In XAML, I set Grid rootElement's background property to that static resource. And finally, I modified the animation, so now it changes the color for that SolidColorBrush. Easy!

How can I create a background color animation with fade?

You can achieve this in XAML. Start animating the Background.Color property of the Window when it's loaded, using a ColorAnimationUsingKeyFrames.

<Window.Background>
<SolidColorBrush x:Name="brush"/>
</Window.Background>
<Window.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard Storyboard.TargetName="brush" Storyboard.TargetProperty="Color">
<ColorAnimationUsingKeyFrames Duration="00:00:05" RepeatBehavior="Forever">
<EasingColorKeyFrame KeyTime="00:00:00" Value="Blue"/>
<EasingColorKeyFrame KeyTime="00:00:01" Value="Red"/>
<EasingColorKeyFrame KeyTime="00:00:02" Value="Yellow"/>
<EasingColorKeyFrame KeyTime="00:00:03" Value="Green"/>
<EasingColorKeyFrame KeyTime="00:00:04" Value="Orange"/>
<EasingColorKeyFrame KeyTime="00:00:05" Value="Blue"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Window.Triggers>

From there and by reading more on animations here and specifically on the ColorAnimationUsingKeyFrames here, you'll be able to tweak it as per your needs and come back with a more specific issue.

WPF Color Animation on data change

I think you need to remove the other active storyboard first, but it's is better to proceed declaring your storyboard in the resources:

<Storyboard x:key="Value 1">
<ColorAnimation
Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)"
To="Red"/>
</Storyboard>

<Storyboard x:key="Value 2">
<ColorAnimation
Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)"
To="Yellow"/>
</Storyboard>

<Storyboard x:key="Value 3">
<ColorAnimation
Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)"
To="Green"/>
</Storyboard>

then look at the datatrigger

<Style x:Key="Colors" TargetType="Label">
<Style.Triggers>

<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Window},
Path=DataContext.ColorNo}" Value="1">
<DataTrigger.EnterActions>
<StopStoryboard BeginStoryboardName="Value2" />
<StopStoryboard BeginStoryboardName="Value3" />
<BeginStoryboard Storyboard="{StaticResource Value1}"
x:Name="Value1" />
</DataTrigger.EnterActions>
</DataTrigger>

...
</Style.Triggers>
</Style>

Do the same for all the triggers

Animate grid background color using storyboard in WPF

Please try this.

<Grid Name="alert_grid" Height="75" HorizontalAlignment="Left" Margin="27,12,0,0" VerticalAlignment="Top" Width="160" Background="LightGray">
<Grid.Triggers>
<EventTrigger RoutedEvent="Button.MouseLeave">
<BeginStoryboard>
<Storyboard x:Name ="flashing_storyboard" Storyboard.Target="{Binding ElementName=alert_grid}">
<ColorAnimation To="Orange" Storyboard.TargetProperty="(Grid.Background).(SolidColorBrush.Color)" From="Black"
AutoReverse="True" RepeatBehavior="Forever" FillBehavior="Stop" Duration="0:0:2"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Grid.Triggers>
</Grid>

and code behind

Storyboard sb = alert_grid.FindName("flashing_storyboard") as Storyboard;
if (sb != null)
{
sb.Begin();
}

WPF how to properly animate color changing

Yeah the problem is .... it IS a "BUG". Triggers are old method of "styling" elements. The new method is using VisualStates

an example would be:

NOTE: this example will not work for you.

<VisualStateManager.VisualStateGroups>
<VisualStateGroup Name="ColorStates">

<VisualState Name="ToGreen">
<Storyboard>
<ColorAnimation To="Green"
Storyboard.TargetProperty="Fill"/>
</Storyboard>
</VisualState>

<VisualState Name="ToBlue">
<Storyboard>
<ColorAnimation To="Blue"
Storyboard.TargetProperty="Fill"/>
</Storyboard>
</VisualState>

<VisualState Name="ToRed">
<Storyboard>
<ColorAnimation To="Red"
Storyboard.TargetProperty="Fill"/>
</Storyboard>
</VisualState>

</VisualStateGroup>
</VisualStateManager.VisualStateGroups>

and then for changing the state you can use a bit c#

 VisualStateManager.GoToState(YOUR_CONTROL, "YOUR_STATE", true);

Or if it's too ugly, you can make a State Behavior

public class StateBehavior 
{
public static readonly DependencyProperty StateProperty =
DependencyProperty.RegisterAttached("State",
typeof(string),
typeof(StateBehavior),
new UIPropertyMetadata(null, StateChanged));

internal static void StateChanged(DependencyObject target, DependencyPropertyChangedEventArgs args)
{
if (args.NewValue != null)
VisualStateManager.GoToState((FrameworkElement)target, args.NewValue, true);
}
}

and use it like this

<YOUR_CONTROL local:StateBehavior.State="{Binding Path=YOUR_STATE_DATA, Mode=TwoWay}" />

BONUS

you can use Transitions for some effects (Ik its off-topic)

PS: Sorry for not showing how to solve your problem. (I'm just too lazy for switching to Windows, I'm on Linux currently)

Animate button background color in XAML

You can use EventTrigger to start ColorAnimation on MouseEnter and MouseLeave:

<ControlTemplate TargetType="{x:Type Button}">
<Border Name="Border" CornerRadius="0" BorderThickness="0" Focusable="False" BorderBrush="Transparent" Background="White">
<ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/>
</Border>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard>
<Storyboard>
<ColorAnimation From="White" To="#52b0ca" Duration="0:0:1" Storyboard.TargetName="Border" Storyboard.TargetProperty="Background.Color"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="MouseLeave">
<BeginStoryboard>
<Storyboard>
<ColorAnimation From="#52b0ca" To="White" Duration="0:0:1" Storyboard.TargetName="Border" Storyboard.TargetProperty="Background.Color"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>

Animated background control in WPF?

I would prefer to animate the background of a border via storyboard. It's pretty easy and you can build a animation as complex as you like. Here is a short example:

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Triggers>
<EventTrigger RoutedEvent="Window.Loaded">
<BeginStoryboard>
<Storyboard AutoReverse="True" BeginTime="0" >
<DoubleAnimation Storyboard.TargetName="Foo"
Storyboard.TargetProperty="Offset"
From="0.2" To="0.8" Duration="0:0:10"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Window.Triggers>
<Border>
<Border.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="Yellow" Offset="0"/>
<GradientStop Color="Orange" Offset="0.2" x:Name="Foo"/>
<GradientStop Color="Red" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<!-- put your windowcontent(grid etc.) here -->
</Border>
</Window>

You should also see the MSDN article Animation Overview.



Related Topics



Leave a reply



Submit