按钮图像路径未在WPF中聚焦

时间:2018-09-24 11:00:20

标签: wpf xaml button

我有一个关闭按钮。我的要求是,当用户的鼠标移到按钮上时,按钮的背景色将改变。

按钮内部有一个用于“ X”符号的路径。当鼠标悬停在路径上时,背景正在正确运行。但是,只要将鼠标悬停在路径上,即可仅更改背景颜色。

所以请任何人建议如何解决这个问题,

MyStyleCode:

<Style x:Key="closeButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="White" />
    <Setter Property="BorderBrush" Value="LightGray" />
    <Setter Property="Foreground" Value="{DynamicResource ForegroundBrush}" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border x:Name="bd">
                    <Path Margin="0"
                          HorizontalAlignment="Center"
                          VerticalAlignment="Center"
                          Data="M0,0 L8,8 M8,0 L0,8"
                          Stroke="White"
                          StrokeThickness="2" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsDefaulted" Value="true">
                        <Setter TargetName="bd" Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter TargetName="bd" Property="Background" Value="Red" />
                    </Trigger>
                    <Trigger Property="IsPressed" Value="true">
                        <Setter TargetName="bd" Property="Background" Value="Red" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

按钮

<Button Grid.Column="1"
        Width="40"
        Height="25"
        Margin="0,0,5,0"
        HorizontalAlignment="Right"
        VerticalAlignment="Center"
        HorizontalContentAlignment="Center"
        Style="{StaticResource closeButtonStyle}"
        ToolTip="Close" />

1 个答案:

答案 0 :(得分:2)

尝试为Style.Triggers设置Button。不只针对您的Content(对于您的情况,Path)。喜欢,

<Style x:Key="closeButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="BorderBrush" Value="LightGray" />
    <Setter Property="Foreground" Value="{DynamicResource ForegroundBrush}" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border x:Name="bd" Background="{TemplateBinding Background}">
                    <Path Margin="0"
                          HorizontalAlignment="Center"
                          VerticalAlignment="Center"
                          Data="M0,0 L8,8 M8,0 L0,8"
                          Stroke="Black"
                          StrokeThickness="2" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsDefaulted" Value="true">
            <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
        </Trigger>
        <Trigger Property="IsMouseOver" Value="true">
            <Setter Property="Background" Value="Red" />
        </Trigger>
        <Trigger Property="Button.IsPressed" Value="true">
            <Setter Property="Background" Value="Red" />
        </Trigger>
    </Style.Triggers>
</Style>