UserControl 样式 - 无法将样式转换为画笔

UserControl style - Unable to cast Style to Brush

我正在处理 WPF 应用程序。我在 shared.xaml 和主题样式中有默认样式的用户控件(一对 XAML 具有不同样式的文件)。

问题是我收到消息 无法将类型 "System.Windows.Style" 的对象转换为类型 "System.Windows.Media.Brush" 并且在用户控件中包含共享样式,如下所示:

Style="{StaticResource statusBar}"

注意:当我删除此行时,没有错误,状态栏中也没有样式(所以问题出在那个样式文件中)。

在shared.xaml

<Style x:Key="statusBar" TargetType="{x:Type UserControl}">
    <Setter Property="Background" Value="{DynamicResource StatusBarBackgroud}"/>
    <Setter Property="FontFamily" Value="{DynamicResource FontFamilyStatusBar}"/>
    <Setter Property="FontWeight" Value="Bold"/>
    <Setter Property="Foreground" Value="{DynamicResource ForegroundStatusBar}"/>
</Style>

问题是前台(在共享文件指向的LightTheme.xaml中)

<Style x:Key="ForegroundStatusBar" >
    <Setter Property="Control.Foreground" Value="LightBlue" />  
</Style>

Foreground 属性 是画笔类型。当 ForegroundStatusBar 资源为 Style 时,您不能在那里分配 {DynamicResource ForegroundStatusBar}。如果前景应继承自基本样式,请在派生样式中使用 BasedOn 参数

<Style x:Key="statusBar" TargetType="{x:Type UserControl}" 
       BasedOn="{StaticResource ForegroundStatusBar}">
    <Setter Property="Background" Value="{DynamicResource StatusBarBackgroud}"/>
    <Setter Property="FontFamily" Value="{DynamicResource FontFamilyStatusBar}"/>
    <Setter Property="FontWeight" Value="Bold"/>
    <!--Foreground setter removed-->
</Style>

试试这个:

<Style x:Key="ForegroundStatusBar"
   TargetType="{x:Type UserControl}">
   <Setter Property="Foreground" Value="LightBlue"></Setter>
</Style>