C# UserControl样式-无法将样式强制转换为笔刷
我正在处理WPF应用程序。我在C# UserControl样式-无法将样式强制转换为笔刷,c#,wpf,xaml,wpf-style,C#,Wpf,Xaml,Wpf Style,我正在处理WPF应用程序。我在shared.xaml中有默认样式的用户控件和主题样式(将xaml文件与不同的样式耦合) 问题是,我收到消息无法将“System.Windows.Style”类型的对象强制转换为“System.Windows.Media.Brush” 在用户控件中,共享样式如下所示: Style="{StaticResource statusBar}" 注意:当我删除这一行时,状态栏中没有错误,也没有样式(所以问题出在样式文件中) 在shared.xaml中 <Style
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中,共享文件指向前台)
前景
属性为笔刷类型。当ForegroundStatusBar资源为样式时,无法在此处分配{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>
<Style x:Key="ForegroundStatusBar"
TargetType="{x:Type UserControl}">
<Setter Property="Foreground" Value="LightBlue"></Setter>
</Style>