WPF按钮仅在文本上方突出显示

时间:2019-08-22 08:16:58

标签: wpf xaml

仅当鼠标悬停在文本上方时按钮才会突出显示,而我希望鼠标悬停在按钮上方时按钮会突出显示。

<Button Content="Characters" Click="BtnClickP1" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="124" Height="50" HorizontalContentAlignment="Left" BorderBrush="{x:Null}" Foreground="White">
            <Button.Style>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="BorderBrush" Value="Transparent"/>
                    <Setter Property="Background" Value="{x:Null}"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type Button}">
                                <Border Background="{TemplateBinding Background}">
                                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="#FF282A30"/>
                        </Trigger>
                        <Trigger Property="IsFocused" Value="True">
                            <Setter Property="Background" Value="#FF282A30"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>

2 个答案:

答案 0 :(得分:1)

您的代码已经可以使用,但是空背景会影响MouseOver事件。

<Button Content="Characters" Click="Button_Click" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="124" Height="50" HorizontalContentAlignment="Left" BorderBrush="{x:Null}" Foreground="White">
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Setter Property="BorderBrush" Value="Transparent"/>
            <Setter Property="Background" Value="Transparent"/> <!-- Here to Transparent -->
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border Background="{TemplateBinding Background}">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="#FF282A30"/>
                </Trigger>
                <Trigger Property="IsFocused" Value="True">
                    <Setter Property="Background" Value="#FF282A30"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

答案 1 :(得分:0)

您必须将背景设置为透明。