How to extend instead of overriding WPF Styles

后端 未结 2 1373
长发绾君心
长发绾君心 2020-12-28 13:26

I want to use custom theme in my application and as far as I know I can accomplish this by using resource dictionary and referencing it in App.xaml. Styles would override th

相关标签:
2条回答
  • 2020-12-28 14:11

    Wpf has different levels of styles, that are applied in order of global > local. A style set directly on a control will override a style set globally, like in your example. I was trying to find a list of all the different places that a control looks for its styles but I cannot find one at the moment. As far as I know, you will have to use the BasedOn property to inherit a style and not completely override the properties of that style with the style you set locally.

    Here is an example of a resource dictionary that has styles based on another style, so that you don't have do repeat the BasedOn binding over and over, you can just set the style on the specific element you want to have that style.

    <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>
    
    0 讨论(0)
  • 2020-12-28 14:18

    I had the same problem. I used Zack's answer and improved it like following so if you don't specify a style the overridden default is still taken in account. It's basically what you would have done but just once in the 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>
    
    0 讨论(0)
提交回复
热议问题