C# WPF - How to modify the ToolBar.ButtonStyleKey style

只愿长相守 提交于 2019-12-24 02:25:20

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/3701263/c-sharp-wpf-how-to-modify-the-toolbar-buttonstylekey-style

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!