C# WPF - 如何修改 ToolBar.ButtonStyleKey 样式

Ben*_*y F 2 c# wpf styles

我需要在鼠标悬停时显示工具栏按钮边框,否则隐藏它。我尝试执行以下操作:

<Style x:Key="{x:Static ToolBar.ButtonStyleKey}" TargetType="Button" BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
    <Setter Property="Foreground" Value="Blue"/>
    <Setter Property="Control.Background" Value="Transparent" />
    <Setter Property="Control.BorderBrush" Value="Transparent" />
    <Setter Property="Control.BorderThickness" Value="1" />
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="VerticalAlignment" Value="Center"/>

    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="true">
            <Setter Property="Control.BorderBrush"  Value="Red"/>
        </Trigger>
    </Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)

但它没有按预期工作。我期望发生的是,鼠标悬停在边框上将变成红色,否则将是透明的。实际结果是它的行为就像具有默认颜色的默认行为一样。
我肯定做错了什么。
有谁知道它是什么?

Zam*_*oni 5

当在 ToolBar 中使用时,尝试以下方法来覆盖由 ToolBar.ButtonStyleKey 标识的按钮样式。

<ToolBar.Resources>
    <Style x:Key="{x:Static ToolBar.ButtonStyleKey}" TargetType="{x:Type Button}">
        <Setter Property="Foreground"
           Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="Padding" Value="2"/>
        <Setter Property="BorderThickness" Value="4"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Template">
           <Setter.Value>
             <ControlTemplate TargetType="{x:Type Button}">
                 <Border x:Name="Bd"
                    SnapsToDevicePixels="true"
                    Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}"
                    Padding="{TemplateBinding Padding}">
                      <ContentPresenter
                         HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                         VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                         SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                  </Border>
                  <ControlTemplate.Triggers>
                     <Trigger Property="IsMouseOver" Value="true">
                       <Setter Property="BorderBrush" TargetName="Bd" Value="Orange"/>
                     </Trigger>
                  </ControlTemplate.Triggers>
             </ControlTemplate>
           </Setter.Value>
        </Setter>
    </Style>
</ToolBar.Resources>
Run Code Online (Sandbox Code Playgroud)