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

发布于 2024-09-19 03:56:08 字数 928 浏览 4 评论 0原文

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

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

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

I have requirement to show the toolbar button border on mouse Hovering and otherwise hide it. I tried to do the following:

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

but it doesn't work as expected. What I expect to happen is that on mouse over the border will turn into red color otherwise will be transparent. Actual result was that it act like in the default behavior with the default colors.
Surely I'm doing something wrong.
Does anyone knows what is it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉 2024-09-26 03:56:08

当在 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>

Try the following to override button style identified by the ToolBar.ButtonStyleKey when used inside a ToolBar.

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文