37

我想在我的应用程序中使用自定义主题,据我所知,我可以通过使用资源字典并在 App.xaml 中引用它来实现这一点。样式会像这样覆盖默认值:

<Style TargetType="{x:Type Label">
    <Setter Property="Foreground" Value="Green" />
</Style>

现在我猜默认标签样式被相同的值覆盖,但我所有的标签字体都是绿色的。当我想再次在某个地方设置一个标签的样式时,问题就开始了。当我想像这样更改网格中的其他属性时

<Grid.Resources>
    <Style TargetType="{x:Type Label">
        <Setter Property="FontSize" Value="28" />
    </Style>
</Grid.Resources>

我的网格内的所有标签都失去了前景色并再次具有默认值(我不是在上一步中覆盖了默认值吗?)。经过一些尝试,我发现要正确执行此操作,我必须在Style声明中添加另一个属性BasedOn={StaticResource {x:Type Label}}"并且它可以工作。这对我来说有点奇怪,因为现在我将不得不在整个应用程序中重复相同的 BasedOn 代码,这不是样式的工作方式 - 这应该自动完成!例如,在 HTML + CSS 中,样式被继承和合并,而在 WPF 中,它们被替换......

请注意,当我不使用任何样式时,控件仍然可以从某个地方(系统主题?)获得它们的外观。我如何告诉他们在其他地方寻找默认值,这样如果没有任何额外的样式代码,他们会认为默认情况下它们应该是绿色的?

有什么方法可以自动设置 BasedOn 属性?或者也许有一个更好的整体做到这一点?

4

2 回答 2

38

我有同样的问题。我使用了 Zack 的答案并对其进行了如下改进,因此如果您不指定样式,仍会考虑覆盖的默认值。这基本上是你会做的,但只在 ResourceDictionary 中做一次。

<Window x:Class="TestWpf.RandomStuffWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Random Stuff Window">
  <Window.Resources>
    <ResourceDictionary>
      <!-- Default Label style definition -->
      <Style TargetType="{x:Type Label}">
        <Setter Property="Foreground" Value="Green" />
      </Style>
      <!-- Extending default style -->
      <Style TargetType="{x:Type Label}" 
             x:Key="LargeGreenForegroundLabel" 
             BasedOn="{StaticResource {x:Type Label}}">
        <Setter Property="FontSize" Value="28" />
      </Style>
    </ResourceDictionary>
  </Window.Resources>
  <StackPanel>
    <Button Click="Button_Click">Click</Button>
    <Label Content="GreenForegroundLabel" /> <!-- Uses default style -->
    <Label Style="{StaticResource LargeGreenForegroundLabel}" 
           Content="LargeGreenForegroundLabel" />
  </StackPanel>
</Window>
于 2016-01-05T11:14:07.753 回答
14

Wpf 有不同级别的样式,按全局 > 本地的顺序应用。直接在控件上设置的样式将覆盖全局样式集,就像在您的示例中一样。我试图找到一个控件查找其样式的所有不同位置的列表,但目前我找不到一个。据我所知,您必须使用 BasedOn 属性来继承样式,而不是使用您在本地设置的样式完全覆盖该样式的属性。

这是一个资源字典的示例,它具有基于另一种样式的样式,因此您不必一遍BasedOn又一遍地重复绑定,您只需在要具有该样式的特定元素上设置样式即可。

<Window x:Class="TestWpf.RandomStuffWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Random Stuff Window">
  <Window.Resources>
    <ResourceDictionary>
      <Style TargetType="{x:Type Label}" 
             x:Key="GreenForegroundLabel">
        <Setter Property="Foreground" Value="Green" />
      </Style>
      <Style TargetType="{x:Type Label}" 
             x:Key="LargeGreenForegroundLabel" 
             BasedOn="{StaticResource GreenForegroundLabel}">
        <Setter Property="FontSize" Value="28" />
      </Style>
    </ResourceDictionary>
  </Window.Resources>
  <StackPanel>
    <Button Click="Button_Click">Click</Button>
    <Label Style="{StaticResource GreenForegroundLabel}" 
           Content="GreenForegroundLabel" />
    <Label Style="{StaticResource LargeGreenForegroundLabel}" 
           Content="LargeGreenForegroundLabel" />
  </StackPanel>
</Window>
于 2013-03-22T17:48:26.597 回答