我无法扩展我在 Windows 字典中定义的一种样式。单独,它似乎按预期将样式应用于我的控件。但是,如果尝试使用 basedOn 属性在我的 userControl 之一中扩展样式,它只会覆盖主要样式,并且所有基本样式都会消失。下面是一个例子:
在名为 dict1.xaml 的资源字典中:
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="Pink"/>
</Style>
在主 window.xaml 中:<Window.Resources>
<ResourceDictionary Source="dict1.xaml"/>
</Window.Resources>
在名为 userControl1.xaml 的用户控件中:<UserControl.Resources>
<Style BasedOn="{StaticResource {x:Type Button}}"
TargetType="{x:Type Button}">
<Setter Property="FontWeight" Value="Bold"/>
</Style>
</UserControl.Resources>
用户控件中的样式简单地覆盖了资源字典中的样式,并且字体为粗体。如果我删除用户控件中的样式,字典中的样式就会生效,背景变成粉红色。我两个都想要。
最佳答案
您需要将字典添加到 UserControl 资源中,或者将其添加到 App.XAML 资源中。
实际上,UserControl 无法解析该 StaticResource——如果有意义的话,它在 UserControl 的范围内,而不是在窗口内。
关于.net - 如何从资源字典继承/扩展样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3619712/