ColorAnimation Gradient On WPF - 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>

Related

WPF border animation not working

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>

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.

Dynamically change the Border Background by setting property in xaml

I'm trying to dynamically change a Border Background property named "Brush2", but I get the following error.
Unable to cast object of type 'System.Windows.Media.Color' to type
'System.Windows.Media.Brush'.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color x:Key="Brush0">#7F605F5F</Color>
<Color x:Key="Brush1">#7F7E7E7E</Color>
<Color x:Key="Color2">#7FFDFDFD</Color>
<SolidColorBrush x:Key="Brush2" Color="{DynamicResource Color2}" />
<Color x:Key="Brush3">#7FFFFFFF</Color>
<Color x:Key="Brush4">#BFFFFFFF</Color>
<Color x:Key="Brush5">#F2FFFFFF</Color>
<Color x:Key="Brush6">#FF6096E5</Color>
<Color x:Key="Brush7">#FF9B7C35</Color>
<Color x:Key="Brush8">#FFB0BAA1</Color>
<Color x:Key="Brush9">#7F60605F</Color>
<Color x:Key="Brush10">White</Color>
<Style TargetType="{x:Type TabItem}" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<!-- silver -->
<VisualState x:Name="Selected">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="border">
<EasingColorKeyFrame KeyTime="0" Value="{DynamicResource Brush8}"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="border">
<EasingColorKeyFrame KeyTime="0" Value="{DynamicResource Brush8}"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="border">
<EasingColorKeyFrame KeyTime="0" Value="{DynamicResource Brush0}"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="border">
<EasingColorKeyFrame KeyTime="0" Value="{DynamicResource Brush9}"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<!-- bronze -->
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="border">
<EasingColorKeyFrame KeyTime="0" Value="{DynamicResource Brush7}"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="border">
<EasingColorKeyFrame KeyTime="0" Value="{DynamicResource Brush7}"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="border">
<EasingColorKeyFrame KeyTime="0" Value="{DynamicResource Brush0}"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="border">
<EasingColorKeyFrame KeyTime="0" Value="{DynamicResource Brush9}"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Flashing">
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="BorderGlass"
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>
<Border x:Name="border" BorderThickness="3" CornerRadius="5,5,0,0">
<Border.BorderBrush>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="{DynamicResource Brush0}" Offset="0"/>
<GradientStop Color="{DynamicResource Brush1}" Offset="1"/>
</LinearGradientBrush>
</Border.BorderBrush>
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="{DynamicResource Brush6}" Offset="0.004"/>
<GradientStop Color="{DynamicResource Brush6}" Offset="0.823"/>
<GradientStop Color="{DynamicResource Brush10}" Offset="0.228"/>
</LinearGradientBrush>
</Border.Background>
</Border>
<Border x:Name="BorderGlass" BorderThickness="3" CornerRadius="5,5,0,0" Background="{DynamicResource Brush2}">
<Border.BorderBrush>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="{DynamicResource Brush3}" Offset="0"/>
<GradientStop Color="{DynamicResource Brush4}" Offset="0.401"/>
<GradientStop Color="{DynamicResource Brush5}" Offset="0.254"/>
</LinearGradientBrush>
</Border.BorderBrush>
</Border>
<ContentPresenter HorizontalAlignment="Center" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" ContentSource="Header"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
What you need to do is set the brush to something like a solidcolorbrush object. http://msdn.microsoft.com/en-us/library/system.windows.media.solidcolorbrush.aspx
The constructor takes a color object.
The following code works, but I had to do this:
Uri uri2 = new Uri("/Resources/DayModeTab2.xaml", UriKind.Relative);
ResourceDictionary resDict2 = Application.LoadComponent(uri2) as ResourceDictionary;
//Make some changes
MobileApp.Get().Resources.MergedDictionaries.Add(resDict2);
And also my poor naming scheme caused conflicts since I have Brush0 in 2 xaml files.
<Border x:Name="BorderGlass" BorderThickness="3" CornerRadius="5,5,0,0" >
<Border.Background>
<SolidColorBrush Color="{DynamicResource Color2}" />
</Border.Background>
<Border.BorderBrush>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="{DynamicResource Brush3}" Offset="0"/>
<GradientStop Color="{DynamicResource Brush4}" Offset="0.401"/>
<GradientStop Color="{DynamicResource Brush5}" Offset="0.254"/>
</LinearGradientBrush>
</Border.BorderBrush>
</Border>

Silverlight 4/ XAML: How can you make Simmilar button styles\templates without copy pasting

Ive made my own button template, which should be in 6 different color's
Now i made 1 style for one of these colors. but the style is really big.
What i want to do is reuse this style also for the other colors
(the styles are all the same except for the colors)
now i was wondering. Is there a way to create these 6 individual styles without copy/pasting it 6 times but sort of bind these colors?
here is the XAML code for clarification:
<UserControl.Resources>
<LinearGradientBrush x:Key="WitNormalBorder" EndPoint="1,1" StartPoint="0,0">
<GradientStop Color="#FFFAFAFF" Offset="0.33"/>
<GradientStop Color="#FFD4D4D7" Offset="1"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="WitNormalFill" EndPoint="1,1" StartPoint="0,0">
<GradientStop Color="#FFD8D8DC" Offset="0"/>
<GradientStop Color="White" Offset="0.66"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="WitOverBorder" EndPoint="1,1" StartPoint="0,0">
<GradientStop Color="White" Offset="0.33"/>
<GradientStop Color="#FFDCDCDE" Offset="1"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="WitOverFill" EndPoint="1,1" StartPoint="0,0">
<GradientStop Color="#FFE6E6E9" Offset="0"/>
<GradientStop Color="White" Offset="0.66"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="WitDownBorder" EndPoint="1,1" StartPoint="0,0">
<GradientStop Color="#FFF0F0FF" Offset="0.33"/>
<GradientStop Color="#FFD2D2D3" Offset="1"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="WitDownFill" EndPoint="1,1" StartPoint="0,0">
<GradientStop Color="#FFDADADC" Offset="0"/>
<GradientStop Color="#FFFAFAFD" Offset="0.66"/>
</LinearGradientBrush>
<Style x:Key="KleurButtonWit" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="Border" BorderThickness="0" Padding="5" CornerRadius="12" Background="{StaticResource WitNormalBorder}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="Border" Storyboard.TargetProperty="(UIElement.Background)">
<DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource WitOverBorder}">
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="Background" Storyboard.TargetProperty="(UIElement.Background)">
<DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource WitOverFill}">
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="(UIElement.Background)">
<DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource WitDownBorder}">
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="Background" Storyboard.TargetProperty="(UIElement.Background)">
<DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource WitDownFill}">
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled">
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused">
</VisualState>
<VisualState x:Name="Unfocused"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="Background" BorderThickness="0" CornerRadius="8" Background="{StaticResource WitNormalFill}">
<ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
as you can see if i have to copy this 6 times it would be a lot of code...
Thanks,
Matthy
You can use the Style.BasedOn property to implement inheritance with styles.
http://msdn.microsoft.com/en-us/library/system.windows.style.basedon.aspx
This should prevent you from having to duplicate the common elements.
If you are going to use these six styles at the same time you will need to copy it six times. I see no way around that.
If you allow the user to select a single style and use only that style at a given point in time this question might be worth a peek also, read this question on loading resources

Resources