control template visibility triggers

独自空忆成欢 提交于 2019-12-10 11:33:12

问题


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

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?

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>

回答1:


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>


来源:https://stackoverflow.com/questions/11000830/control-template-visibility-triggers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!