按钮的 WPF 控件样式未检测到 IsMouseOver

WPF Control Style of Button not detecting IsMouseOver

所以我尝试使用 ResourceDictionary 创建按钮样式(背景不透明度为黑色,alpha 为 20%,文本的默认颜色更改为纯白色)。我确实将文件包含到 App.xaml 中,例如:

        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Style/ButtonStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>

我正在向按钮应用 x:KeyStyle="{StaticResource TopBarButtons}"

所以我的风格看起来像(随机颜色只是为了测试):

    <Style x:Key="TopBarButtons" TargetType="{x:Type Button}">
        <Setter Property="Background" Value="HotPink"></Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border CornerRadius="5" Background="{TemplateBinding Background}" BorderThickness="1" Padding="5">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Foreground" Value="Red" />
                <Setter Property="Background" Value="Lime" />
            </Trigger>
        </Style.Triggers>
    </Style>

但是 none 正在检测,没有任何变化。我的错误是什么?

设置

<Button ... Background="Transparent"/>

直接在 Button 上设置 so-called 本地值 ,其优先级高于样式 Setter 或 [=24] 设置的值=] 在样式触发器中。

任何应由样式触发器更改的值只能由样式初始化 Setter,例如

<Setter Property="Background" Value="Transparent"/>

参考 Dependency property value precedence