gpt4 book ai didi

wpf - 按钮图像路径未在 wpf 中聚焦

转载 作者:行者123 更新时间:2023-12-04 19:46:32 24 4
gpt4 key购买 nike

我有一个关闭按钮。我的要求是,当用户鼠标经过按钮时,按钮背景颜色会改变。

我在“X”符号的按钮内有一条路径。当鼠标悬停在路径上时,背景正在改变,工作正常。但是非常接近鼠标在路径上然后只有背景颜色被改变。

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

我的样式代码:

<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" />

最佳答案

尝试为您的 Button 设置 Style.Triggers。不要单独针对您的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>

关于wpf - 按钮图像路径未在 wpf 中聚焦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52477872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com