2010-09-13 42 views
1

我有要求显示鼠标悬停和否则隐藏它的工具栏按钮边框。我试图做到以下几点:C#WPF - 如何修改ToolBar.ButtonStyleKey风格

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

但它不能正常工作。我期望发生的事情是,鼠标移到边界上会变成红色,否则就会变成透明的。实际结果是,它的行为就像使用默认颜色的默认行为。
当然,我做错了什么。
有谁知道它是什么?

回答

4

请尝试以下操作以覆盖在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>