2016-12-23 72 views
0

如何刪除/更改MouseOver上的MenuItem/ContextMenu的hilghlighted顏色?我試圖編輯ContextMenu模板併爲MouseOver上的MenuItem設置樣式,但默認的HiglightedColor在MouseOver上是stil。如何在MouseOver上刪除/更改MenuItem/ContextMenu的hilghlighted顏色?

<Style TargetType="{x:Type ContextMenu}"> 
        <Setter Property="SnapsToDevicePixels" 
       Value="True" /> 
        <Setter Property="OverridesDefaultStyle" 
       Value="True" /> 
        <Setter Property="Grid.IsSharedSizeScope" 
       Value="true" /> 
        <Setter Property="HasDropShadow" 
       Value="True" /> 
        <Setter Property="Template"> 
         <Setter.Value> 
          <ControlTemplate TargetType="{x:Type ContextMenu}"> 
           <Border x:Name="Border" 
        Background="Black" 
        BorderThickness="0"> 
            <Border.BorderBrush> 
             <SolidColorBrush Color="Black" /> 
            </Border.BorderBrush> 
            <StackPanel IsItemsHost="True" 
          KeyboardNavigation.DirectionalNavigation="Cycle" /> 
           </Border> 
           <ControlTemplate.Triggers> 
            <Trigger Property="HasDropShadow" 
         Value="true"> 
             <Setter TargetName="Border" 
         Property="Padding" 
         Value="0,0,0,0" /> 
             <Setter TargetName="Border" 
         Property="CornerRadius" 
         Value="0" /> 
            </Trigger> 
            <Trigger Property="IsMouseOver" Value="True"> 
             <Setter Property="BorderBrush" Value="Transparent"/> 
             <Setter Property="Background" Value="Transparent"/> 
            </Trigger> 
           </ControlTemplate.Triggers> 
          </ControlTemplate> 
         </Setter.Value> 
        </Setter> 
       </Style> 

<Style TargetType="{x:Type MenuItem}"> 
        <Style.Triggers> 
         <Trigger Property="IsHighlighted" Value="True"> 
          <Setter Property="Background" Value="Transparent"></Setter> 
         </Trigger> 
         </Trigger> 
         <Trigger Property="IsMouseOver" Value="true"> 
          <Setter Property="Background" Value="Transparent"> 

          </Setter> 
         </Trigger> 
        </Style.Triggers> 
       </Style> 

回答

1

您必須覆蓋MenuItem默認模板,並設置觸發IsHighlighted屬性:

<ContextMenu> 
    <ContextMenu.Resources> 
     <Style TargetType="{x:Type MenuItem}"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type MenuItem}"> 
         <Border Background="{TemplateBinding Background}"> 
          <ContentPresenter Content="{TemplateBinding Header}" Margin="35 5 10 5" /> 
         </Border> 
         <ControlTemplate.Triggers> 
          <Trigger Property="IsHighlighted" Value="True"> 
           <Setter Property="Background" Value="Green"/> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </ContextMenu.Resources> 
    <MenuItem Header="Element 1" /> 
    <MenuItem Header="Element 2" /> 
    <MenuItem Header="Element 3" /> 
</ContextMenu> 

去除背景項目時徘徊只設置背景顏色爲Transparent

相關問題