.net - 如何从资源字典继承/扩展样式?

标签 .net wpf xaml wpf-controls app.xaml

我无法扩展我在 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/

相关文章:

c# - 如何使用属性更改 TimePicker 格式

c# - WPF 的替代品?

c# - 使用 BindingContext 在 StackLayout 中绑定(bind)标签的 TextColor

c# - 按钮 FontWeight 不起作用

c# - 检查数据表中是否有空值的最佳方法

C#: 求PNG压缩算法/库

.net - 手机上的 MVC4 BrowserOverride.Desktop 问题

.net - 片段和模板有什么区别?

c# - 将数据从属性绑定(bind)到文本 block - MVVM Light 和 WPF

c# - 如何转换 DateTimeOffset?到 C# 中的日期时间?