2010-09-13 307 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>