我对下面的按钮样式所做的只是让按钮仅在 IsMouseOver 或 IsPressed 时可见。
它的编写方式甚至无法编译,找不到“Glyph”。我怎样才能清理这个按钮,使其在 IsMoueOver 时可见?
干杯,
贝里尔
<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。
<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>
关于wpf - 控制模板可见性触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11000830/