我有一个包含颜色对象的UserControl作为资源:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
...
</ResourceDictionary.MergedDictionaries>
<Color x:Key="BackgroundColor">Transparent</Color>
</ResourceDictionary>
</UserControl.Resources>
此UserControl中有一个Grid作为根元素,它有一个自定义样式,我想在其中引用上面的颜色资源:
<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>
每次运行应用程序时,我都会在框架尝试设置样式时获得XAML解析异常。如果我将BackgroundColor
作为StaticResource引用它可以正常工作,但我在某些条件下更改了代码中的资源,因此必须动态引用它。
如何在此上下文中将资源用作动态资源?