WPF如何为一个按钮增强或继承两种样式

时间:2013-11-26 10:16:36

标签: c# wpf

基于此链接How do you completely remove the button border in wpf?按钮的边框可以通过以下方式完全删除,其工作原理与我想要的完全相同:

<Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">SomeText</Button>

现在我想在我的ResourceDictionary中定义一些额外的样式,但我不确定如何引用另一种样式。通常我通过以下方式引用自定义样式:

 <Button Style="someSelfDefinedStyle">SomeText</Button>

如果我想删除按钮的边框并在其上添加其他样式,我该怎么办?

更新

我也尝试过以下方式:

<Style x:Key="someSelfDefinedStyle" BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
    <!--Add additional styling here-->
</Style>

它给了我以下错误:

'System.Windows.SystemThemeKey' cannot be converted to type 'System.Windows.Style'

1 个答案:

答案 0 :(得分:3)

您需要使用Style.BasedOn属性为您执行此操作:

<Style x:Key="AnotherStyle" BasedOn="{StaticResource someSelfDefinedStyle}">
    <!--Add additional styling here-->
</Style>

更新&gt;&gt;&gt;

要使用默认 Style作为BasedOn属性值,您可以这样做:

<Style x:Key="AnotherStyle" BasedOn="{StaticResource {x:Type Button}}">
    <!--Add additional styling here-->
</Style>

但是,您不能使用ToolBar.ButtonStyleKey,因为它是object而不是Style。元素的DefaultStyleKey用于在运行时查找相关元素的默认Style

如果你想在Button上进一步设置样式,你有两个选择......第一个是为Button定义一个新的ControlTemplate,第二个是放一个代替BorderButton的{​​{1}}元素:

Style

但当然......你有点回到你开始的地方。