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:
- If I move the definition of
MyColor
from the window resources to application resources, the code works as expected. - 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. - The behaviour in .NET 4/WPF 4 is the same.
- 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, becauseLinearColorKeyFrame
is notFrameworkElement
. Maybe this is root of the problem?