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