Search code examples
.netwpfstylesdynamicresource

DynamicResource color not working


I have the following code:

<Color x:Key="SelectedColor">Gold</Color> 

And a TabItem Style that contains the color

<VisualState x:Name="Selected">
    <Storyboard>
        <ColorAnimationUsingKeyFrames 
            Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)"
            Storyboard.TargetName="InnerRectangle2">
            <EasingColorKeyFrame KeyTime="0" Value="{DynamicResource SelectedColor}"/>
        </ColorAnimationUsingKeyFrames>

It turns out I can't use a DynamicResource on an EasingColorKeyFrame.
What can I do to achieve my effect?

I need to set the color dynamically, so just swapping "{DynamicResource SelectedColor}" with "{StaticResource SelectedColor}" is off the table.

I've created a tiny solution to demonstrate the problem - the Selected Tab should be Gold colored, but it's actually transparent, because I guess the VSM can't resolve the color named "SelectedColor"

http://dl.dropbox.com/u/10557283/DynamicBug.zip


Solution

  • I figured out a way to do it with layers. Make multiple copies of your object, and then just modify the transparencies like this:

    <VisualState x:Name="Selected">
        <Storyboard>
            <DoubleAnimation Storyboard.TargetName="InnerRectangleBorder"
                             Storyboard.TargetProperty="Opacity"
                             To="0"
                             Duration="0:0:0" />
    
            <DoubleAnimation Storyboard.TargetName="InnerRectangleBorderMouseOver"
                             Storyboard.TargetProperty="Opacity"
                             To="0"
                             Duration="0:0:0.5" />
    
            <DoubleAnimation Storyboard.TargetName="InnerRectangleBorderSelected"
                             Storyboard.TargetProperty="Opacity"
                             To="1"
                             Duration="0:0:1" />
        </Storyboard>
    </VisualState>