1

Dear all, I've got the following problem with WPF 3.5. When accessing a dynamic resource in a storyboard, the value is wrong (seemingly the reference is not resolved) when the resource is defined in the window which contains the target control.

What am I doing wrong, and is there a right way (or at least a workaround)? Basically I want the storyboard to use the colors from dynamic resource, and I can only put those resources locally at the window level.

Example code:

<Window
    x:Class="Test.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"
    MouseDoubleClick="Window_MouseDoubleClick">
    <Window.Resources>
        <Color x:Key="MyColor">Blue</Color>
    </Window.Resources>
    <Grid x:Name="outer">
        <Grid.Resources>
            <Storyboard x:Key="MyBoard">
                <ColorAnimationUsingKeyFrames
                    Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                    <LinearColorKeyFrame KeyTime="0:0:0" Value="Red"/>
                    <LinearColorKeyFrame KeyTime="0:0:1" Value="{DynamicResource MyColor}"/>
                </ColorAnimationUsingKeyFrames>
            </Storyboard>
            <SolidColorBrush x:Key="MyBrush" Color="{DynamicResource MyColor}"/>
        </Grid.Resources>
        <Grid x:Name="inner" Background="Green"/>
    </Grid>
</Window>

Code-behind:

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }

    private void Window_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        Storyboard s = (Storyboard)outer.FindResource("MyBoard");
        inner.BeginStoryboard(s);
        //SolidColorBrush b = (SolidColorBrush)outer.FindResource("MyBrush");
        //inner.Background = b;
    }
}

The problem is that the reference Value="{DynamicResource MyColor}" is not working, the value is transparent.

I tried the following:

  1. If I move the definition of MyColor from the window resources to application resources, the code works as expected.
  2. If I replace the storyboard start with assigning of another resource (comment out first two lines in Window_MouseDoubleClick, and uncomment the rest), the dynamic resource access works.
  3. The behaviour in .NET 4/WPF 4 is the same.
  4. I tried creating the storyboard from the code behind (just for test), but cannot convert the line <LinearColorKeyFrame KeyTime="0:0:1" Value="{DynamicResource MyColor}"/> into procedural code, because LinearColorKeyFrame is not FrameworkElement. Maybe this is root of the problem?
Vlad
  • 35,022
  • 6
  • 77
  • 199

1 Answers1

1

I don't know if there's a way to do it with XAML and binding, I couldn't get a similar thing working in Silverlight, but as a workaround you can create the StoryBoard and animations in code: http://msdn.microsoft.com/en-us/library/cc189069(VS.95).aspx#procedural_code

dain
  • 6,475
  • 1
  • 38
  • 47
  • I didn't find the way to do the same from code, because `LinearColorKeyFrame` is not a `FremeworkElement`, just a `DependencyObject`, therefore I cannot use `SetResourcereference` to assign the dynamic resource reference to a `LinearColorKeyFrame`. So I cannot translate the code `` into procedural code. Is there a way to do this? – Vlad Nov 26 '10 at 11:53
  • Have you tried using ColorAnimation instead of LinearColorKeyFrame? See http://msdn.microsoft.com/en-us/library/system.windows.media.animation.coloranimation.aspx and http://www.c-sharpcorner.com/UploadFile/rahul4_saxena/WPFColorAnimation09072009030353AM/WPFColorAnimation.aspx – dain Nov 26 '10 at 11:59
  • Actually this might also work: http://stackoverflow.com/questions/2248726/wpf-coloranimation-for-a-brush-property – dain Nov 26 '10 at 12:01
  • @dain: thanks a lot, replacing `ColorAnimationUsingKeyFrames` with just a `ColorAnimation` solved the problem! Looks like the frames are not a part of visual tree, while the animation itself is. – Vlad Nov 26 '10 at 12:15
  • Good stuff, glad it helped and thanks for the info about the visual tree, I didn't know that! So that means even the original binding works in WPF if you use a non-frame based animation? – dain Nov 26 '10 at 12:44
  • @dain: yes, just using the animation w/o keyframes solved the problem, it works with the original bindings. – Vlad Dec 03 '10 at 14:25