WPF border animation not working - wpf

I am trying to put an animation on a border as follows
<LinearGradientBrush x:Key="RedButtonBrushUp" StartPoint=".5,0" EndPoint=".5,1">
<GradientStop Color="#ffaaaa" Offset="0"/>
<GradientStop Color="#cc6666" Offset="0.6"/>
<GradientStop Color="#bb2222" Offset="1"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="ButtonBrushUp" StartPoint=".5,0" EndPoint=".5,1">
<GradientStop Color="#aaccff" Offset="0"/>
<GradientStop Color="#7799ff" Offset="0.6"/>
<GradientStop Color="#555599" Offset="1"/>
</LinearGradientBrush>
<Border x:Name="BorderUp" BorderThickness="2,2,2,2" CornerRadius="4,4,4,4" Background="{StaticResource RedButtonBrushUp}">
<Border.Triggers>
<EventTrigger RoutedEvent="Border.Loaded">
<BeginStoryboard>
<Storyboard >
<DoubleAnimation Storyboard.TargetName="BorderUp"
Storyboard.TargetProperty="Background"
RepeatBehavior="Forever"
AutoReverse="True"
To="{StaticResource ButtonBrushUp}"
Duration="0:0:0.5" ></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Border.Triggers>
</Border>
But it says {StaticResource ButtonBrushUp} is an invalid type for this. What am I doing wrong?

The To can't accept such a change needed for the gradient brush stops.
Here is a working example of what you are trying to do
<Window.Resources>
<Storyboard x:Key="OnLoaded1">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="myBorder">
<EasingColorKeyFrame KeyTime="0:0:1" Value="#FFC53838"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
<Window.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded" SourceName="myBorder">
<BeginStoryboard Storyboard="{StaticResource OnLoaded1}"/>
</EventTrigger>
</Window.Triggers>
<Grid>
<StackPanel x:Name="stackPanel" Orientation="Vertical" HorizontalAlignment="Center">
<Border x:Name="myBorder" Background="#99FFFFFF" BorderThickness="3"
Margin="0,60,0,20" Padding="20" >
<Border.BorderBrush>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Border.BorderBrush>
<TextBlock>
This example shows how to have a border change color after an initial load.
</TextBlock>
</Border>
</StackPanel>

Related

Unset databound property when animation completes in WPF

I would like to set my databound property IsFlashing to False when my animation completes.
<Border BorderThickness="1"
Height="15"
Width="20"
CornerRadius="2"
BorderBrush="Black"
DockPanel.Dock="Top"
Grid.Row="1" >
<Border.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding IsFlashing}" Value="True" x:Name="dataTrigger">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard FillBehavior="Stop" RepeatBehavior="1x" x:Name="startStoryBoard">
<ObjectAnimationUsingKeyFrames
Storyboard.TargetProperty="Background"
Duration="0:0:4" RepeatBehavior="1x">
<ObjectAnimationUsingKeyFrames.KeyFrames>
<DiscreteObjectKeyFrame KeyTime="0:0:1">
<DiscreteObjectKeyFrame.Value>
<LinearGradientBrush>
<LinearGradientBrush.GradientStops>
<GradientStop Color="Yellow" Offset="0.0" />
<GradientStop Color="Orange" Offset="0.9" />
<GradientStop Color="Red" Offset="1.0" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames.KeyFrames>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<!-- how can I set my bound property "IsFlashing" to false when we exit here ? -->
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<Border.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="#FFFFFF" Offset="0.0" />
<GradientStop Color="#ADD8E6" Offset="0.9" />
<GradientStop Color="#8aacb8" Offset="1.0" />
</LinearGradientBrush>
</Border.Background>
<TextBlock Foreground="Black" Text="{Binding Path=MarkerCode}" FontFamily="Segoe UI" FontSize="12" TextAlignment="Center" VerticalAlignment="Center">
<TextBlock.Effect>
<DropShadowEffect ShadowDepth="0" BlurRadius="3" Color="White" />
</TextBlock.Effect>
</TextBlock>
</Border>
Listen to the Completed event of the ObjectAnimationUsingKeyFrames
ExitActions are what happens when the trigger condition is no longer met (in this case when IsFlashing returns to false), which is not the same the animation completing.

EventTrigger RoutedEvent amimation in wpf xaml

I want an animation to be launched when MyObject.IsGlowing = true so I did this (I'm in a <DataTemplate> if that can help)
<Ellipse Width="100" Height="100" Name="MyEllipse">
<Ellipse.Fill>
<RadialGradientBrush >
<GradientStop Offset="0" Color="Red" />
<GradientStop Offset="0.5" Color="red" />
<GradientStop Offset="1" Color="red"/>
</RadialGradientBrush >
</Ellipse.Fill>
<Ellipse.Triggers>
<DataTrigger Binding="{Binding Source=IsGlowing}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Name="MyBeginStoryBoard">
<Storyboard Name="MyStoryBoard" >
//Animation
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Ellipse.Triggers>
</Ellipse>
but I got this error "Triggers collection members must be of type EventTrigger" so I changed it to
<Ellipse Width="100" Height="100" Name="MyEllipse">
<Ellipse.Fill>
<RadialGradientBrush >
<GradientStop Offset="0" Color="Red" />
<GradientStop Offset="0.5" Color="red" />
<GradientStop Offset="1" Color="red"/>
</RadialGradientBrush >
</Ellipse.Fill>
<Ellipse.Triggers>
<EventTrigger RoutedEvent="?">
<BeginStoryboard Name="MyBeginStoryBoard">
<Storyboard Name="MyStoryBoard" >
//Animation
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Ellipse.Triggers>
</Ellipse>
But i have no idea what to put in RoutedEvent...
You aren't allowed DataTriggers to be directly used/specified within an elements Triggers collection. However, you can use a Style to apply one.
http://www.thejoyofcode.com/Help_Why_cant_I_use_DataTriggers_with_controls_in_WPF.aspx

VisualStateManager.GoToState with TabItem not working

My tabitem is styled by a ResourceDictionary that has a VisualState named Flashing.
I want to do something like this, but its not working.
public static void FlashTab(TabItem tbi)
{
bool thisReturnsFalse = VisualStateManager.GoToState(tbi, "Flashing", true);
}
The XAML
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color x:Key="TabBrush0">#7F605F5F</Color>
<Color x:Key="TabBrush01">#7F605F5F</Color>
<Color x:Key="TabBrush02">#7F605F5F</Color>
<Color x:Key="TabBrush03">#7F605F5F</Color>
<Color x:Key="TabBrush04">#7F605F5F</Color>
<Color x:Key="TabBrush1">#7F7E7E7E</Color>
<Color x:Key="TabBrush2">#7FFDFDFD</Color>
<Color x:Key="TabBrush3">#7FFFFFFF</Color>
<Color x:Key="TabBrush4">#BFFFFFFF</Color>
<Color x:Key="TabBrush5">#F2FFFFFF</Color>
<Color x:Key="TabBrush6">#FF6096E5</Color>
<Color x:Key="TabBrush7">#FF9B7C35</Color>
<Color x:Key="TabBrush8">#FFB0BAA1</Color>
<Color x:Key="TabBrush10">White</Color>
<Color x:Key="Foreground">#FF000000</Color>
<Color x:Key="MouseOverColor">Purple</Color>
<Color x:Key="SelectedColor">Blue</Color>
<SolidColorBrush x:Key="ForegroundBrush" Color="{DynamicResource Foreground}" />
<Style TargetType="{x:Type TabItem}" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid Margin="1,0,1,0" >
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="InnerRectangleMouseOver">
<EasingDoubleKeyFrame KeyTime="0" Value=".2"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Selected">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="InnerRectangleSelected">
<EasingDoubleKeyFrame KeyTime="0" Value=".2"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Flashing" >
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="GlassCube"
Storyboard.TargetProperty="Opacity"
From="0.0"
To="1.0"
Duration="0:0:0.33"
AutoReverse="True"
RepeatBehavior="Forever"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Rectangle x:Name="InnerRectangleMouseOver" Margin="1,1,1,1" RadiusY="5" RadiusX="5" StrokeThickness="3" Opacity="0">
<Rectangle.Effect>
<BlurEffect/>
</Rectangle.Effect>
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
<GradientStop Color="{DynamicResource MouseOverColor}" Offset="0.228"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle x:Name="InnerRectangleSelected" Margin="1,1,1,1" RadiusY="5" RadiusX="5" StrokeThickness="3" Opacity="0">
<Rectangle.Effect>
<BlurEffect/>
</Rectangle.Effect>
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
<GradientStop Color="{DynamicResource SelectedColor}" Offset="0.228"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle x:Name="InnerRectangle" Margin="1,1,1,1" RadiusY="5" RadiusX="5" StrokeThickness="3" >
<Rectangle.Effect>
<BlurEffect/>
</Rectangle.Effect>
<Rectangle.Stroke>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="{DynamicResource Brush1}" Offset="1"/>
<GradientStop Color="{DynamicResource Brush0}"/>
</LinearGradientBrush>
</Rectangle.Stroke>
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
<GradientStop Color="{DynamicResource Brush13}" Offset="0.228"/>
<GradientStop Color="{DynamicResource Brush7}"/>
<GradientStop Color="{DynamicResource Brush7}" Offset="0.823"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle x:Name="GlassCube" Margin="1,1,1,1" RadiusY="5" RadiusX="5" StrokeThickness="3" Opacity="1">
<Rectangle.Stroke>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="{DynamicResource Brush4}"/>
<GradientStop Color="{DynamicResource Brush4}"/>
<GradientStop Color="{DynamicResource Brush6}" Offset="0.254"/>
<GradientStop Color="{DynamicResource Brush5}" Offset="0.401"/>
</LinearGradientBrush>
</Rectangle.Stroke>
<Rectangle.OpacityMask>
<RadialGradientBrush>
<GradientStop Color="{DynamicResource Brush11}" Offset="0"/>
<GradientStop Color="{DynamicResource Brush13}" Offset="1"/>
</RadialGradientBrush>
</Rectangle.OpacityMask>
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="{DynamicResource Brush12}"/>
<GradientStop Color="{DynamicResource Brush7}" Offset="0.47"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<ContentPresenter HorizontalAlignment="Center" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" ContentSource="Header" >
<TextBlock.Foreground>
<SolidColorBrush Color="{DynamicResource Foreground}"/>
</TextBlock.Foreground>
</ContentPresenter>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
The TabItem will flash when you merge the ResourceDictionary into the Window.Resources of the TabControl's parent window (the ResourceDictionary is in ResourceDictionary.xaml here):
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
and when you fix the OpacityMask of the GlassCuberectangle. I've commented it out and it happily flashes:
<Rectangle x:Name="GlassCube" Margin="1,1,1,1" RadiusY="5" RadiusX="5" StrokeThickness="3" Opacity="1">
<Rectangle.Stroke>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="{DynamicResource TabBrush4}"/>
<GradientStop Color="{DynamicResource TabBrush4}"/>
<GradientStop Color="{DynamicResource TabBrush6}" Offset="0.254"/>
<GradientStop Color="{DynamicResource TabBrush5}" Offset="0.401"/>
</LinearGradientBrush>
</Rectangle.Stroke>
<!--<Rectangle.OpacityMask>
<RadialGradientBrush>
<GradientStop Color="{DynamicResource TabBrush11}" Offset="0"/>
<GradientStop Color="{DynamicResource TabBrush13}" Offset="1"/>
</RadialGradientBrush>
</Rectangle.OpacityMask>-->
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="{DynamicResource TabBrush12}"/>
<GradientStop Color="{DynamicResource TabBrush7}" Offset="0.47"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
EDIT: There is one more thing to mention concerning the keys of the color resources. You define them with keys TabBrush1, TabBrush2 etc. but when you use them in Color="{DynamicResource ...}", you always use keys Brush1, Brush2 etc.

How to animate the StartPoint of LinearGradient

I have a LinearGradientBrush used as an OpacityMask and I want my animation to rotate the gradient, so I'm trying to animate the position of the StartPoint and EndPoint but I can't make it work for hours :(
<Style x:Key="NewContentStyle" TargetType="ContentPresenter">
<Setter Property="OpacityMask">
<Setter.Value>
<LinearGradientBrush x:Name="FillGradient" EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0.5"/>
<GradientStop Color="Transparent" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
<Storyboard x:Key="NewContentStoryboard">
<PointAnimation Storyboard.TargetProperty="StartPoint" Storyboard.TargetName="FillGradient" From="0.5 0" To="0 0.5" Duration="00:00:1" />
<PointAnimation Storyboard.TargetProperty="EndPoint" Storyboard.TargetName="FillGradient" From="1 0.5" To="0 0.5" Duration="00:00:1"/>
</Storyboard>
I get an error "'FillGradient' name cannot be found in the name scope of 'System.Windows.Controls.ContentPresenter'."
Please try this:
<Storyboard x:Key="NewContentStoryboard">
<PointAnimation Storyboard.TargetProperty="OpacityMask.(LinearGradientBrush.StartPoint)" From="0.5 0" To="0 0.5" Duration="00:00:1" />
<PointAnimation Storyboard.TargetProperty="OpacityMask.(LinearGradientBrush.EndPoint)" From="1 0.5" To="0 0.5" Duration="00:00:1"/>
</Storyboard>

ColorAnimation Gradient On WPF

<LinearGradientBrush x:Key="BrushPrincipalBorderBlue" EndPoint="1.3,1" StartPoint="-0.2,0">
<GradientStop Color="#FF030637" Offset="1"/>
<GradientStop Color="#FF0E0F31" Offset="0.166"/>
<GradientStop Color="#FF2E2F70" Offset="0.629"/>
<GradientStop Color="#FF030637" Offset="0.63"/>
</LinearGradientBrush>
<Storyboard x:Key="GoToBlue">
<ColorAnimation Storyboard.TargetName="border" Storyboard.TargetProperty="Background.GradientStops[3].Color" Duration="0:0:0.5"
To="{DynamicResource BrushPrincipalBorderBlue.GradientStops[3].Color}" />
</Storyboard>
How i can Set in the 'To' property to an GradientStop Color of my brush? because calling the way im calling it doesnt work.
The BrushPrincipalBorderBlue name is just a key it is not an actual LinearGradientBrush therefore it does not have any Propertys. I was able to get the error to go away by adding a name to the Brush. See if that works for you.
<Window.Resources>
<LinearGradientBrush x:Name="myBrush" x:Key="BrushPrincipalBorderBlue" EndPoint="1.3,1" StartPoint="-0.2,0">
<GradientStop Color="#FF030637" Offset="1"/>
<GradientStop Color="#FF0E0F31" Offset="0.166"/>
<GradientStop Color="#FF2E2F70" Offset="0.629"/>
<GradientStop Color="#FF030637" Offset="0.63"/>
</LinearGradientBrush>
<Storyboard x:Key="GoToBlue">
<ColorAnimation Storyboard.TargetName="border" Storyboard.TargetProperty="Background.GradientStops[3].Color" Duration="0:0:0.5" To="{DynamicResource myBrush.GradientStops[3].Color}" />
</Storyboard>
The only way I was able to access the colors was to define them seperatly i.e.
<Window.Resources>
<Color x:Key="BrushPrincipalBorderBlue.Color1">
<Color.A>#FF</Color.A>
<Color.B>#03</Color.B>
<Color.R>#06</Color.R>
<Color.G>#37</Color.G>
</Color>
<Color x:Key="BrushPrincipalBorderBlue.Color2">
<Color.A>#FF</Color.A>
<Color.B>#0E</Color.B>
<Color.R>#0F</Color.R>
<Color.G>#31</Color.G>
</Color>
<Color x:Key="BrushPrincipalBorderBlue.Color3">
<Color.A>#FF</Color.A>
<Color.B>#2E</Color.B>
<Color.R>#2F</Color.R>
<Color.G>#70</Color.G>
</Color>
<Color x:Key="BrushPrincipalBorderBlue.Color4">
<Color.A>#FF</Color.A>
<Color.B>#03</Color.B>
<Color.R>#06</Color.R>
<Color.G>#37</Color.G>
</Color>
<LinearGradientBrush x:Key="BrushPrincipalBorderBlue" EndPoint="1.3,1" StartPoint="-0.2,0">
<GradientStop Color="{DynamicResource BrushPrincipalBorderBlue.Color1}" Offset="1"/>
<GradientStop Color="{DynamicResource BrushPrincipalBorderBlue.Color2}" Offset="0.166"/>
<GradientStop Color="{DynamicResource BrushPrincipalBorderBlue.Color3}" Offset="0.629"/>
<GradientStop Color="{DynamicResource BrushPrincipalBorderBlue.Color4}" Offset="0.63"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="BrushPrincipalBorderOriginal" EndPoint="1.3,1" StartPoint="-0.2,0">
<GradientStop Color="#FFFFFFFF" Offset="1"/>
<GradientStop Color="#FFFFFFFF" Offset="0.166"/>
<GradientStop Color="#FFFFFFFF" Offset="0.629"/>
<GradientStop Color="#FFFFFFFF" Offset="0.63"/>
</LinearGradientBrush>
</Window.Resources>
<Grid Name="myGrid" Background="{DynamicResource BrushPrincipalBorderOriginal}" >
<Grid.Triggers>
<EventTrigger RoutedEvent="Canvas.Loaded">
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="myGrid" Storyboard.TargetProperty="Background.GradientStops[3].Color" Duration="0:0:2" To="{DynamicResource BrushPrincipalBorderBlue.Color3}" AutoReverse="True" RepeatBehavior="Forever"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Grid.Triggers>
</Grid>
Not sure what you want to do but Offset is a double number, hence you need to use DoubleAnimation like this one:
<Canvas.Triggers>
<EventTrigger RoutedEvent="Canvas.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="BlackCanvasGradient"
Storyboard.TargetProperty="Offset"
From="0" To="0.8" Duration="0:0:2" >
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Canvas.Triggers>

Resources