2016-12-07 118 views
2

我是WPF的初學者。因此,我需要了解邊界與內容之間的差距。我也有這個問題按鈕以及。WPF MenuItem懸停樣式填充問題

總之,我有一個MenuItem的樣式,如下所示,問題是當我將鼠標懸停在ItemItem和在MenuItem邊緣和內容之間移動指針時,邊框將消失。我認爲填充或邊距有問題。

我希望邊框在指針位於項目內時始終可見。

<Style TargetType="MenuItem" x:Key="MenuItemsStyle"> 
    <Setter Property="BorderThickness" Value="1"/> 
    <Setter Property="BorderBrush" Value="Transparent"/> 
    <Setter Property="Background" Value="Transparent"/> 
    <Setter Property="Padding" Value="3,0,3,0"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="MenuItem"> 
       <Border Margin="{TemplateBinding Margin}" 
         x:Name="B1" 
         BorderBrush="{TemplateBinding BorderBrush}" 
         BorderThickness="{TemplateBinding BorderThickness}" 
         SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"> 
        <Grid VerticalAlignment="Center"> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="Auto"/> 
          <ColumnDefinition Width="Auto"/> 
         </Grid.ColumnDefinitions> 
         <ContentPresenter 
          x:Name="Textb" 
          Grid.Column="1" 
          Content="{TemplateBinding Header}" 
          ContentStringFormat="{TemplateBinding HeaderStringFormat}" 
          ContentSource="Header" 
          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
          VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
          HorizontalAlignment="{TemplateBinding HorizontalAlignment}" 
          TextElement.Foreground="Black"> 
          <ContentPresenter.ContentTemplate> 
           <DataTemplate> 
            <Border> 
             <Grid> 
              <TextBlock Text="{TemplateBinding Property=Content}"/> 
             </Grid> 
            </Border> 
           </DataTemplate> 
          </ContentPresenter.ContentTemplate> 
         </ContentPresenter> 
        </Grid> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsHighlighted" Value="True"> 
        </Trigger> 
        <Trigger Property="IsEnabled" Value="False"> 
        </Trigger> 
        <MultiTrigger> 
         <MultiTrigger.Conditions> 
          <Condition Property="IsEnabled" Value="True"/> 
          <Condition Property="IsHighlighted" Value="True"/> 
         </MultiTrigger.Conditions> 
         <Setter Property="BorderBrush" TargetName="B1" Value="Gray"/> 
        </MultiTrigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 

     </Setter.Value> 
    </Setter> 
</Style> 

回答

1

你應該設置你最BorderBackground財產。

<ControlTemplate TargetType="MenuItem"> 
    <Border Margin="{TemplateBinding Margin}" 
      x:Name="B1" 
      Background="Transparent" 
      ... 
    </Border> 
    ... 
</ControlTemplate> 
+0

謝謝@ qqww2 –