4

我无法扩展我在 Windows 字典中定义的一种样式。单独,它似乎按预期将样式应用于我的控件。但是,如果尝试使用 basedOn 属性在我的一个用户控件中扩展样式,它只会覆盖主要样式,并且所有基本样式都会消失。这是一个例子:

在名为 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>

用户控件中的样式简单地覆盖资源字典中的样式,字体为粗体。如果我删除用户控件中的样式,字典中的样式就会生效并且背景变成粉红色。我两个都想要。

4

1 回答 1

1

您要么需要将字典添加到 UserControl 资源,要么将其添加到 App.XAML 资源。

实际上,UserControl 无法解析该 StaticResource - 如果有意义的话,它在 UserControl 的范围内,而不是窗口的范围内。

于 2010-09-01T16:35:52.103 回答