locked
Work around for Color="{StaticResource SomeBrush.Color}? RRS feed

  • Question

  • I have a SolidColorBrush in my resource collection:

    <SolidColorBrush x:Key="SomeBrush" Color="White" />

    I later on use the following in an animation:

    <ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="ButtonColor" Storyboard.TargetProperty="Color" To="{StaticResource SomeColor}"/>

    I understand that I cannot get access to Color of SomeBrush as "SomeColor.Color" is not in the dictionary, but does anyone know of a way of getting the Color from a SolidColorBrush thats a static resource?

    I'm using a SolidColorBrush and not a Color because I need to modify the SomeBrush.Color elsewhere in my code. I did try

    <Color x:Key="SomeColor">#ffffff</Color>

    <ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="ButtonColor" Storyboard.TargetProperty="Color" To="{StaticResource SomeColor}"/>

    Which did work, but I could not modify SomeColor in my code#

    So to recap, is there either

    a) A way to get map Color from a SolidColorBrush thats a static resource

    b) A way to modify a Color thats in the static resource dictionary

    Oh and the ColorAnimations are inside a ControlTemplate. Heres the complete template:

     

    1    <!-- Button Colours -->
    2    <Color x:Key="ButtonBGS0">#2020bD</Color>
    3    <Color x:Key="ButtonBGS1">#6060bD</Color>
    4    <Color x:Key="ButtonBGS0_MouseOver">#2020bD</Color>
    5    <Color x:Key="ButtonBGS1_MouseOver">#8080e0</Color>
    6    <Color x:Key="ButtonBGS0_Pressed">#b01010</Color>
    7    <Color x:Key="ButtonBGS1_Pressed">#e04040</Color>
    8    
    9    
    10   <!-- Button Templates -->
    11   <ControlTemplate x:Key="ButtonTemplate" TargetType="Button">
    12   	<Grid>
    13   		<VisualStateManager.VisualStateGroups>
    14   			<VisualStateGroup x:Name="CommonStates">
    15   				<VisualState x:Name="MouseOver">
    16   					<Storyboard>
    17   						<ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="ButtonBackgroundBrushS0" Storyboard.TargetProperty="Color" To="{StaticResource ButtonBGS0_MouseOver}"/>
    18   						<ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="ButtonBackgroundBrushS1" Storyboard.TargetProperty="Color" To="{StaticResource ButtonBGS1_MouseOver}"/>
    19   						<DoubleAnimation Duration="0:0:0.2" Storyboard.TargetName="ButtonScale" Storyboard.TargetProperty="ScaleX" To="1.05"/>
    20   						<DoubleAnimation Duration="0:0:0.2" Storyboard.TargetName="ButtonScale" Storyboard.TargetProperty="ScaleY" To="1.05"/>
    21   					</Storyboard>
    22   				</VisualState>
    23   				<VisualState x:Name="Pressed">
    24   					<Storyboard>
    25   						<ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="ButtonBackgroundBrushS0" Storyboard.TargetProperty="Color" To="{StaticResource ButtonBGS0_Pressed}"/>
    26   						<ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="ButtonBackgroundBrushS1" Storyboard.TargetProperty="Color" To="{StaticResource ButtonBGS1_Pressed}"/>
    27   						<DoubleAnimation Duration="0:0:0.2" Storyboard.TargetName="ButtonScale" Storyboard.TargetProperty="ScaleX" To="0.95"/>
    28   						<DoubleAnimation Duration="0:0:0.2" Storyboard.TargetName="ButtonScale" Storyboard.TargetProperty="ScaleY" To="0.95"/>
    29   					</Storyboard>
    30   				</VisualState>
    31   				<VisualState x:Name="Disabled">
    32   					<Storyboard>
    33   						<ColorAnimation Duration="0:0:0" Storyboard.TargetName="ButtonBackgroundBrushS0" Storyboard.TargetProperty="Color" To="#808080"/>
    34   						<ColorAnimation Duration="0:0:0" Storyboard.TargetName="ButtonBackgroundBrushS1" Storyboard.TargetProperty="Color" To="#808080"/>
    35   						<DoubleAnimation Duration="0:0:0" Storyboard.TargetName="ButtonScale" Storyboard.TargetProperty="ScaleX" To="1.0"/>
    36   						<DoubleAnimation Duration="0:0:0" Storyboard.TargetName="ButtonScale" Storyboard.TargetProperty="ScaleY" To="1.0"/>
    37   					</Storyboard>
    38   				</VisualState>
    39   				<VisualState x:Name="Normal">
    40   					<Storyboard>
    41   						<ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="ButtonBackgroundBrushS0" Storyboard.TargetProperty="Color" To="{StaticResource ButtonBGS0}"/>
    42   						<ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="ButtonBackgroundBrushS1" Storyboard.TargetProperty="Color" To="{StaticResource ButtonBGS1}"/>
    43   						<DoubleAnimation Duration="0:0:0" Storyboard.TargetName="ButtonScale" Storyboard.TargetProperty="ScaleX" To="1.0"/>
    44   						<DoubleAnimation Duration="0:0:0" Storyboard.TargetName="ButtonScale" Storyboard.TargetProperty="ScaleY" To="1.0"/>
    45   					</Storyboard>
    46   				</VisualState>
    47   			</VisualStateGroup>
    48   		</VisualStateManager.VisualStateGroups>
    49   		<Border BorderBrush="#b0b0b0" BorderThickness="2" CornerRadius="6" RenderTransformOrigin="0.5,0.5" >
    50   			<Border.RenderTransform>
    51   				<ScaleTransform x:Name="ButtonScale" ScaleX="1.0" ScaleY="1.0"></ScaleTransform>
    52   			</Border.RenderTransform>
    53   			<Border.Background>
    54   				<LinearGradientBrush x:Name="ButtonBackgroundBrush">
    55   					<LinearGradientBrush.GradientStops>
    56   						<GradientStop x:Name="ButtonBackgroundBrushS0" Offset="0.00" Color="{StaticResource ButtonBGS0}" />
    57   						<GradientStop x:Name="ButtonBackgroundBrushS1" Offset="1.00" Color="{StaticResource ButtonBGS1}" />
    58   					</LinearGradientBrush.GradientStops>
    59   				</LinearGradientBrush>
    60   			</Border.Background>
    61   			<ContentPresenter 
    62   			Content="{TemplateBinding Content}"
    63   			ContentTemplate="{TemplateBinding ContentTemplate}"
    64   			Margin="{TemplateBinding Margin}"
    65   			HorizontalAlignment="Center"
    66   			VerticalAlignment="Center">
    67   			</ContentPresenter>
    68   		</Border>
    69   	</Grid>
    70   </ControlTemplate>
    
     
    Monday, July 27, 2009 3:00 PM

Answers

  • Thanks for your reply. I tried your idea but it crashes :It may be because the animation is inside a control template

    Tuesday, July 28, 2009 5:07 AM

All replies

  • I thought of an idea but I dont know if it works or not coz I dont have a "working" computer to test it right now. My idea should try on Silverlight 3.

    give a name to your SolidColorBrush let say _ColorBrush

    then bind the color of the colorbrush to the "TO" value

    <ColorAnimation ......        To="{Binding Color, ElementName=_ColorBrush}"   ... />

     

    Monday, July 27, 2009 3:27 PM
  • Thanks for your reply. I tried your idea but it crashes :It may be because the animation is inside a control template

    Tuesday, July 28, 2009 5:07 AM
  • For anyone thats interested, I resolved the issue by resorting to using brushes and replacing:

    <ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="ButtonBackgroundBrushS0" Storyboard.TargetProperty="Color" To="{StaticResource ButtonBGS0_MouseOver}"/>

    <ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="ButtonBackgroundBrushS1" Storyboard.TargetProperty="Color" To="{StaticResource ButtonBGS1_MouseOver}"/>

    With:

    <LinearGradientBrush x:Key="ButtonBG_MouseOver">

    <LinearGradientBrush.GradientStops>

    <GradientStop Offset="0.00" Color="{StaticResource ButtonBGS0_MouseOver}" />

    <GradientStop Offset="1.00" Color="{StaticResource ButtonBGS1_MouseOver}" />

    </LinearGradientBrush.GradientStops>

    </LinearGradientBrush>

    <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:1" Storyboard.TargetName="ButtonBorder" Storyboard.TargetProperty="Background">

    <DiscreteObjectKeyFrame KeyTime="00:00:0" Value="{StaticResource ButtonBG_MouseOver}" />

    </ObjectAnimationUsingKeyFrames>

     Not as nice but does the trick

     

     

    Tuesday, August 4, 2009 6:09 AM