I have a UserControl that contains a color object as resource:
<UserControl.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> ... </ResourceDictionary.MergedDictionaries> <Color x:Key="BackgroundColor">Transparent</Color> </ResourceDictionary> </UserControl.Resources> There is a Grid as root element in this UserControl, that has a custom style in which I would like to reference the color resource above:
<Grid.Style> <Style TargetType="Grid"> <Style.Triggers> <Trigger Property="FrameworkElement.IsMouseOver" Value="True"> <Trigger.EnterActions> <BeginStoryboard> <Storyboard> <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)"> <SplineColorKeyFrame KeyTime="0:0:0.5" KeySpline="0.15,0.8 0.3,1" Value="LightBlue" /> </ColorAnimationUsingKeyFrames> </Storyboard> </BeginStoryboard> </Trigger.EnterActions> <Trigger.ExitActions> <BeginStoryboard> <Storyboard> <!-- Here is the problem --> <ColorAnimation To="{DynamicResource BackgroundColor}" Duration="0:0:1" Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" /> </Storyboard> </BeginStoryboard> </Trigger.ExitActions> </Trigger> </Style.Triggers> </Style> </Grid.Style> Each time I run the app I get a XAML parse exception when the framework tries to set the style. If I reference the BackgroundColor as StaticResource it works just fine, but I'm changing the resource in code behind at certain conditions so it has to be referenced dynamically.
How can I use the resource as a dynamic resource in this context?