C# WPF在样式内的数据触发器内使用动态资源
我有一个UserControl,其中包含一个颜色对象作为资源:C# WPF在样式内的数据触发器内使用动态资源,c#,wpf,xaml,dynamicresource,C#,Wpf,Xaml,Dynamicresource,我有一个UserControl,其中包含一个颜色对象作为资源: <UserControl.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> ... </ResourceDictionary.MergedDictionaries> <Color x:Key="BackgroundColor
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
...
</ResourceDictionary.MergedDictionaries>
<Color x:Key="BackgroundColor">Transparent</Color>
</ResourceDictionary>
</UserControl.Resources>
...
透明的
此UserControl中有一个网格作为根元素,具有自定义样式,我希望在其中引用上面的颜色资源:
<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,它工作得很好,但是我在某些条件下更改了代码中的资源,因此必须动态引用它
如何在此上下文中将资源用作动态资源?可能相关:可能相关: