wpf 如何根据资源字典中的另一种样式创建样式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5675004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:47:50  来源:igfitidea点击:

How to base a style on another style in a resource dictionary?

wpfxamlstyles

提问by Marcom

I have a theme that is applied to all buttons in a resource dictionary. Now I want to add a trigger to the button while inheriting the style changes from the dictionary. I tried the following code, but it says that the control cannot be found. How can I fix it ?

我有一个应用于资源字典中所有按钮的主题。现在我想在从字典继承样式更改的同时向按钮添加触发器。我尝试了以下代码,但它说找不到控件。我该如何解决?

<UserControl.Resources>
  <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary Source="Theme.xaml"/>
      </ResourceDictionary.MergedDictionaries>

      <conv:ErrorContentConverter x:Key="ErrorContentConverter" />

      <Style x:Key="ValidTrigger" 
             TargetType="Control" BasedOn="{StaticResource {x:Type Control}}">
         <Style.Triggers>
            <DataTrigger Binding="{Binding Path=IsValid}" Value="False">
              <Setter Property="IsEnabled" Value="false" />
            </DataTrigger>
         </Style.Triggers>
      </Style>   
  </ResourceDictionary>
</UserControl.Resources>

The base template:

基础模板:

    <Style TargetType="{x:Type Button}" BasedOn="{x:Null}">
    <Setter Property="FocusVisualStyle" 
            Value="{DynamicResource NuclearButtonFocusVisual}" />
    <Setter Property="Foreground" Value="#FF042271" />
    <Setter Property="FontFamily" Value="Trebuchet MS" />
    <Setter Property="FontSize" Value="12" />
    <Setter Property="Padding" Value="3" />

    <Setter Property="Template" Value="{DynamicResource ButtonTemplate}" />
</Style>

回答by Nathan Friend

One trick I've used in the past: in your ResourceDictionarythat defines blanket styles for your application, add an x:Keyto the style you'd like to inherit from, like so:

我过去使用过的一个技巧:在你ResourceDictionary为你的应用程序定义一揽子样式中,x:Key向你想要继承的样式添加一个,如下所示:

<Style TargetType="{x:Type Button}" x:Key="ButtonStyle">
  <!-- your style here -->
</Style>

To apply this style to all controls of the specified type (Buttonin this example) without having to explicitly set the Styleattribute of every Button, add another style that's BasedOnthis style, but doesn't have a key:

要将此样式应用于指定类型的所有控件(Button在此示例中)而不必显式设置Styleevery的属性Button,请添加另一个样式,即BasedOn此样式,但没有键:

<Style TargetType="{x:Type Button}" BasedOn="{StaticResource ButtonStyle}" />

Using this method, all Buttons in your application will automatically inherit your custom style, but you can still create additional styles BasedOnthe ButtonStyleentry and avoid blowing away all of your custom styling.

使用这种方法,Button应用程序中的所有s 将自动继承您的自定义样式,但您仍然可以BasedOnButtonStyle条目创建其他样式并避免吹散所有自定义样式。

回答by Pieter Müller

Give your base Style a name, say FooStyle.

为您的基本样式命名,例如 FooStyle。

In the example you gave, modify the TargetType and BasedOn to look as follows:

在您提供的示例中,将 TargetType 和 BasedOn 修改为如下所示:

 <Style x:Key="ValidTrigger" 
        TargetType="{x:Type Control}" BasedOn="{StaticResource {x:Type Control}}" >
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=IsValid}" Value="False">
            <Setter Property="IsEnabled" Value="false" />
        </DataTrigger>
    </Style.Triggers>
</Style>

回答by Mike Schenk

I think there is no base style defined for "control" so your BasedOn="{StaticResource {x:Type Control}}"part won't find anything.

我认为没有为“控制”定义基本样式,因此您的 BasedOn="{StaticResource {x:Type Control}}"部分将找不到任何东西。

You probably want to change

你可能想改变

<Style x:Key="ValidTrigger" TargetType="Control" BasedOn="{StaticResource {x:Type Control}}" >

to

<Style x:Key="ValidTrigger" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}" >