我知道这是重复的,但即使在尝试复制我在类似帖子中看到的代码后,我似乎仍然无法让按钮在悬停时更改其背景颜色。 这是 XAML。
<Window x:Class="Wolfram_Editor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Wolfram_Editor"
mc:Ignorable="d"
Height="450" Width="800"
Background="#242526"
WindowStyle="None"
AllowsTransparency="True"
Title="Wolfram Editor"
ResizeMode="CanResizeWithGrip"
>
<Window.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="Black"/>
<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="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height ="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="1" Grid.ColumnSpan="4" Background="Black" MouseDown="Grid_MouseDown">
<TextBlock
Grid.Column="1"
Foreground="DarkGray"
HorizontalAlignment="Center"
FontSize="15"
Text="Unitiled.wlf"/>
<StackPanel HorizontalAlignment="Right" Grid.Column="2" Orientation="Horizontal">
<Button
Name="ButtonMinimize"
Grid.Column="2"
Background="Black"
Foreground="DarkGray"
Width="20"
BorderThickness="0"
HorizontalAlignment="Right"
FontSize="15"
Click="ButtonMinimize_Click">
-</Button>
<Button
Name="ButtonResize"
Grid.Column="2"
Background="Black" Foreground="DarkGray"
Width="20"
BorderThickness="0"
HorizontalAlignment="Right"
FontSize="15"
Click="ButtonResize_Click">
☐</Button>
<Button
Name="ButtonClose"
Grid.Column="2"
Background="Black"
Foreground="DarkGray"
Width="20"
BorderThickness="0"
HorizontalAlignment="Right"
FontSize="15"
Click="ButtonClose_Click">
✕</Button>
</StackPanel>
</Grid>
<Button
Grid.ColumnSpan="2"
Name="ButtonFile"
Foreground="DarkGray"
Background="#191919"
Width="50"
HorizontalAlignment="Left"
BorderThickness="0">
File</Button>
<Border Background="#3e3e42" Grid.Row="1"/>
<TextBox
Name="Editor"
Grid.Column="1"
Grid.ColumnSpan="2"
Grid.Row="1"
Background="#191919"
AcceptsReturn="True"
AcceptsTab="True"
Foreground="White"
SpellCheck.IsEnabled="True"
VerticalScrollBarVisibility="Auto"
FontSize="{Binding Zoom, UpdateSourceTrigger=PropertyChanged}"
/>
</Grid>
</Window>
该按钮不会改变其背景颜色,即使悬停时也保持不变,但否则应该会变成红色。这些按钮能够捕获点击事件并按预期工作。
有什么原因吗?
将鼠标悬停在 WPF 中的触发器上不会更改按钮的背景颜色可能有多种原因。以下是一些英文的潜在原因:
不正确的触发器配置:触发器可能未在按钮的样式或控件模板中正确设置。确保在适当的资源内正确定义触发器。
“触发器可能未在按钮的样式或控件模板中正确配置,这可能会阻止悬停时背景颜色的变化。” 未满足触发条件:可能未满足触发器中指定的条件。例如,可以将触发器设置为响应与悬停状态不同的属性或状态。
“未满足触发器中指定的条件,可能是因为触发器未设置为响应悬停状态。” 未应用样式或模板:包含触发器的自定义样式或控件模板可能不会应用于按钮。
“包含触发器的自定义样式或控件模板可能不会应用于按钮,从而阻止发生悬停效果。” 覆盖样式:另一种样式或模板可能会覆盖包含触发器的样式或模板,从而导致触发器被忽略。
“可能会优先考虑被覆盖的样式或模板,这可能会覆盖触发器并阻止背景颜色更改。” 未处理鼠标事件:如果按钮未设置为正确处理鼠标事件,则可能无法识别悬停状态。
“按钮可能无法正确处理鼠标事件,这是识别悬停状态并触发颜色变化所必需的。” 控件被禁用或只读:如果按钮被禁用或只读,它可能不会响应悬停事件。
“如果按钮被禁用或只读,它可能不会响应悬停事件,因此背景颜色不会改变。” 资源不匹配:触发器的资源键可能与按钮样式或模板中使用的资源键不匹配。
“触发器和按钮的样式或模板之间的资源键可能不匹配,导致触发器无法被识别。” 调试问题:代码中可能存在 XAML 中不可见的错误或错误,这可能会阻止触发器工作。
“代码中隐藏的错误或错误可能会阻止触发器按预期运行。” 请务必检查您的 XAML 和任何相关的代码隐藏文件,以确保所有必要的配置均正确,并且不存在可能导致问题的冲突或覆盖。