本文介绍了控制模板可见性触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有我正在尝试使用以下按钮样式,只有当IsMouseOver或IsPressed才可以看到该按钮。

All I am trying to do with the button style below is have the button only be visible when either IsMouseOver or IsPressed.

写入的方式甚至不会编译,找不到雕文。在IsMoueOver?可以看到这个按钮是如何清理的?

The way it's written won't even compile, not finding "Glyph". How can I clean this up to the button is visible when IsMoueOver?

干杯,

Berryl

Cheers,
Berryl

<Style x:Key="EditCommandButtonStyle" TargetType="{x:Type Button}">
   <Setter Property="Content">
      <Setter.Value>
         <TextBlock x:Name="Glyph" Width="30"
            FontFamily="Wingdings 3" FontSize="24" Text="a" Visibility="Hidden"/>
      </Setter.Value>
   </Setter>
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="{x:Type Button}">
            <Border x:Name="Border" Background="Transparent" CornerRadius="4">
               <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Border>
            <ControlTemplate.Triggers>
               <Trigger Property="IsMouseOver" Value="True">
                  <Setter TargetName="Border" Property="Background" Value="LightBlue"/>
                  <Setter TargetName="Glyph" Property="Visibility" Value="Visible"/>
               </Trigger>
               <Trigger Property="IsPressed" Value="True">
                  <Setter TargetName="Border" Property="Background" Value="Orange"/>
               </Trigger>
            </ControlTemplate.Triggers>
         </ControlTemplate>
      </Setter.Value>
   </Setter>
</Style>


推荐答案

使ContentPresenter成为命名目标,而不是TextBlock。

Make the ContentPresenter the named target instead of the TextBlock.

<Style x:Key="EditCommandButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Content">
        <Setter.Value>
            <TextBlock FontFamily="Wingdings 3" FontSize="24" Text="a" />
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border x:Name="Border" Background="Transparent" CornerRadius="4">
                    <ContentPresenter x:Name="theContent" Visibility="Hidden"
                           HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="Border" Property="Background"
                                Value="LightBlue"/>
                        <Setter TargetName="theContent" Property="Visibility"
                                Value="Visible"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter TargetName="Border" Property="Background"
                                Value="Orange"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这篇关于控制模板可见性触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 09:25