【问题标题】:How to make Style.Triggers trigger a different named style to be applied如何使 Style.Triggers 触发要应用的不同命名样式
【发布时间】:2010-11-14 02:45:16
【问题描述】:

假设我有以下内容:

<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>

这很好用,这里没有什么太大的问题,但这是一个相当简单的案例。如果我想将 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>

【问题讨论】:

    标签: wpf triggers styles setter


    【解决方案1】:

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

    <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>
    

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

    【讨论】:

      【解决方案2】:

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

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

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

      然后为 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>
      

      托尼

      【讨论】:

      • 这对我来说是个例外:“Style 对象不允许影响它所应用的对象的 Style 属性。”很明显,一种风格如何改变自己的风格,但仍然可以继续工作?
      • 我可以发誓我的应用程序中有一个这样的例子,但我现在找不到它。我不记得在我的开发过程中发生的错误,这并不意味着什么,真的。当我有几分钟的空闲时间时,我将不得不玩这个。
      • @svick:我已经编辑了我的示例,所以它现在使用的代码有效。它现在可能与原始问题不完全相关,因为它使用控件模板和默认样式。但这确实有效。
      【解决方案3】:

      WPF 正在为此 FrameworkElement.FocusVisualStyle 提供一个特殊属性,所以继续分配它:)

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

      或者使用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>
      

      【讨论】:

        【解决方案4】:

        我的情况涉及需要在按钮的视图框中显示锁定/解锁图标图像。不幸的是,视图框上没有 Content 的直接属性。根据@Tony 的上述回复,这是一个工作示例,其中包含他对我的情况的回答。

        <Button Padding="5 0" Margin="0" Cursor="Hand" Style="{StaticResource IconButton}">
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" >
                <Viewbox Stretch="Fill" Height="20" Width="20">
                    <ContentControl >
                        <ContentControl.Resources>
                            <ControlTemplate x:Key="Open" TargetType="ContentControl">
                                <ContentControl Style="{StaticResource LockOpenIcon}" />
                            </ControlTemplate>
                            <ControlTemplate x:Key="Closed" TargetType="ContentControl">
                                <ContentControl Style="{StaticResource LockClosedIcon}" />
                            </ControlTemplate>
                        </ContentControl.Resources>
                        <ContentControl.Style>
                            <Style TargetType="{x:Type ContentControl}" >
                                <Setter Property="Template" Value="{StaticResource Open}" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Path=WorkflowAccessibility, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" Value="{x:Static da:GatewayWorkflowAccessibility.VisibleDisabled}">
                                        <Setter Property="Template" Value="{StaticResource Closed}" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </ContentControl.Style>
                    </ContentControl>
                </Viewbox>
            </StackPanel>
        </Button>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-12-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-24
          相关资源
          最近更新 更多