如何提供;“附加”;样式设置为控件,但将继承的值保留在WPF中?
在许多情况下,我想应用“小”样式,但仍然“受益于”全局设置样式 一个小例子:如何提供;“附加”;样式设置为控件,但将继承的值保留在WPF中?,wpf,xaml,Wpf,Xaml,在许多情况下,我想应用“小”样式,但仍然“受益于”全局设置样式 一个小例子: <Windows.Resources> <!-- a "small" style that only modifies a very small detail --> <Style x:Key="S1" TargetType="Button"> <Setter Property="Background" Value="Yellow"/&
<Windows.Resources>
<!-- a "small" style that only modifies a very small detail -->
<Style x:Key="S1" TargetType="Button">
<Setter Property="Background" Value="Yellow"/>
</Style>
<!-- the style I want to use in addition-->
<Style TargetType="Button">
<Setter Property="Foreground" Value="Blue"/>
</Style>
</Windows.Resources>
<!-- this Button should have a yellow background and a blue foreground -->
<Button Style="{StaticResource ResourceKey=S1}">S1</Button>
S1
如果我想应用两种样式,我有一个基于的解决方案,但它不适用于这里,因为一种样式是隐式的
我也不能基于其他样式创建样式,因为样式
S1
不知道自动样式,自动样式也应该应用于其他不使用S1
的控件,我最近遇到了类似的问题,我解决了以下问题:
我将键BaseButtonStyle添加到基本样式中,该样式应该重复使用
<Window.Resources>
<Style x:Key="BaseButtonStyle" TargetType="Button">
<Setter Property="Foreground" Value="Blue"/>
</Style>
<Style x:Key="S1" TargetType="Button" BasedOn="{StaticResource BaseButtonStyle}">
<Setter Property="Background" Value="Yellow"/>
</Style>
</Window.Resources>
在我创建的其他Windows/UserControls/UI元素中,BaseButtonStyle应该自动分配
<Window.Resources>
<Style TargetType="Button" BasedOn="{StaticResource BaseButtonStyle}" />
</Window.Resources>
我刚刚发现了一个适合我的解决方案(因为我想保留默认的系统样式,但只是调整了一些次要方面) 其实质是建立在系统风格的基础上。以下是基本代码部分:
<Style TargetType=”TextBox” BasedOn=”{StaticResource {x:Type TextBox}}”>
我想将“小”样式与隐式样式合并,隐式样式可能取决于windows版本。虽然你的版本在其他情况下可能会有所帮助,但我认为在我的情况下不会。