wpf 如何使 Style.Triggers 触发要应用的不同命名样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1183982/
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 20:46:25  来源:igfitidea点击:

How to make Style.Triggers trigger a different named style to be applied

wpftriggersstylessetter

提问by vdhant

Lets say I have the below:

假设我有以下内容:

<Style TargetType="{x:Type TextBox}">
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="BorderBrush" Value="Gray" />
    <Style.Triggers>
        <Trigger Property="IsFocused" Value="true"> 
            <Setter Property="BorderBrush" Value="Green" />
            <Setter Property="BorderThickness" Value="2" />
        </Trigger>
    </Style.Triggers> 
</Style>

This works fine and there is nothing too much wrong here, but it is a fairly simple case. What happens if I want to have the IsFocused style state listed as a exsplicit style how do reference that style as being the IsFocused style, i.e.

这工作正常,这里没有什么太大的问题,但这是一个相当简单的案例。如果我想将 IsFocused 样式状态列为显式样式,会发生什么情况如何将该样式引用为 IsFocused 样式,即

<Style x:key="ActiveStyle" TargetType="{x:Type TextBox}">
    <Setter Property="BorderBrush" Value="Green" />
    <Setter Property="BorderThickness" Value="2" />
</Style>

<Style TargetType="{x:Type TextBox}">
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="BorderBrush" Value="Gray" />
    <Style.Triggers>
        <Trigger Property="IsFocused" Value="true">
           -- Here I want to reference ActiveStyle and not copy the copy the setters
        </Trigger>
    </Style.Triggers> 
</Style>

采纳答案by Nicolas Dorier

I don't think you can however, you can reuse a style this way :

但是,我认为您不能,您可以通过这种方式重用样式:

<Style x:Key="ActiveStyle" TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <Trigger Property="IsFocused" Value="true">
            <Setter Property="BorderBrush" Value="Green" />
            <Setter Property="BorderThickness" Value="2" />
        </Trigger>
    </Style.Triggers>
</Style>

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource ActiveStyle}">
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="BorderBrush" Value="Gray" />
</Style>

I don't see another solution :(

我没有看到其他解决方案:(

回答by Jobi Joy

WPF is providing a special property for this FrameworkElement.FocusVisualStyle So go ahead and assign that :)

WPF 为这个 FrameworkElement.FocusVisualStyle 提供了一个特殊的属性所以继续分配它:)

<TextBox FocusVisualStyle="{StaticResource ActiveStyle}" .....

Or another way using setters

或者使用 setter 的另一种方式

<Style TargetType="{x:Type TextBox}">
 <Setter Property="BorderThickness" Value="1" />
 <Setter Property="BorderBrush" Value="Gray" />    
 <Setter Property="FocusVisualStyle" >
  <Setter.Value>
    <Style x:key="ActiveStyle" TargetType="{x:Type TextBox}">
       <Setter Property="BorderBrush" Value="Green" />
       <Setter Property="BorderThickness" Value="2" />
    </Style>
   </Setter.Value>
  </Setter>
 </Style>

回答by Tony Vitabile

There is yet a third way to do this.

还有第三种方法可以做到这一点。

Create two named control templates for your control:

为您的控件创建两个命名控件模板:

<ControlTemplate x:Key="NotFocused" TargetType="{x:Type TextBox}">  
    . . .
</ControlTemplate>  

<ControlTemplate x:Key="Focused" TargetType="{x:Type TextBox}">   
    . . .
</ControlTemplate>

Then you create a default style for the TextBox with the triggers in it:

然后为 TextBox 创建一个默认样式,其中包含触发器:

<Style TargetType="{x:Type TextBox}">   
    <Style.Triggers>
        <Trigger Property="IsFocused" Value="True">
            <Setter Property="Template" Value="{StaticResource Focused}" />
        </Trigger>
        <Trigger Property="IsFocused" Value="False">
            <Setter Property="Template" Value="{StaticResource NotFocused}" />
        </Trigger>
    </Style.Triggers>
</Style>

Tony

托尼