2010-06-05 78 views
12

我已經添加了一個默認的菜單控件到我的用戶控件。我需要設置菜單的樣式以去除包含圖標或複選框空間的左邊距。我怎樣才能做到這一點?WPF - 如何樣式菜單控件刪除左邊距?

XAML:

<Menu> 
    <MenuItem Header="MyMenu" FontSize="10"> 
     <MenuItem Header="Options..." /> 
     <MenuItem Header="About" /> 
    </MenuItem> 
</Menu> 

目前,它呈現像任何其他的菜單控制開箱:

alt text

我不想保證金或列在菜單的左邊項目。這通常用於圖標等等。

+0

你能提供一些你的XAML的? – jsmith 2010-06-05 04:29:05

+0

請詳細解釋您的問題並向我們提供您的XAML。 – 2010-06-05 06:27:18

+0

我已經添加了XAML和默認渲染的屏幕截圖。菜單的使用在很大程度上是無關緊要的,我從問題中刪除了該文本。 – 2010-06-05 12:42:05

回答

3

這不是很簡單的,但你需要創建一個MenuItemStyle,最簡單的通過Expression Blend中:

<Menu> 
    <MenuItem Header="MyMenu" Style="{DynamicResource MenuItemStyle1}"> 
     <MenuItem Header="Options..." /> 
     <MenuItem Header="About" /> 
    </MenuItem> 
</Menu> 

它創建了一個非常詳細的一套模板和樣式,和您需要編輯菜單項以移除網格的固定寬度第一列,然後在SubMenuBorder ContentControl模板中刪除形成背景着色的矩形。我附上了一個示例項目,刪除了邊距。 Download sample project here.

+0

謝謝。這將是幾天,但我會檢查了這一點。 – 2010-06-14 16:13:01

+2

它適用於普通菜單,但不適用於ContextMenu。仍然有一個垂直線分隔圖標和菜單標題。 :( – 2013-10-01 22:26:53

+0

404錯誤的鏈接。 – David 2015-03-04 12:57:09

3

兩個選項:

  1. 短,簡單和直接的。根據您使用的是何種菜單(請參閱details),爲MenuItemContextMenu設置ItemsPanelTemplate

  2. 激進。從零開始重寫Menu風格。有兩種準備使用樣式:

4

我認爲這就是你所追求的(再次,使用Expression Blend來解決它,但是它可以在它顯示的內容方面獲得最簡約的效果......並且花了很多時間來玩) ...你可以博士

<Window x:Class="MenuItemWithNoIcon.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <SolidColorBrush x:Key="MenuItem.Highlight.Background" Color="#3D26A0DA"/> 
     <SolidColorBrush x:Key="MenuItem.Highlight.Border" Color="#FF26A0DA"/> 
     <SolidColorBrush x:Key="Menu.Disabled.Foreground" Color="#FF707070"/> 
     <SolidColorBrush x:Key="MenuItem.Highlight.Disabled.Background" Color="#0A000000"/> 
     <SolidColorBrush x:Key="MenuItem.Highlight.Disabled.Border" Color="#21000000"/> 
     <SolidColorBrush x:Key="MenuItem.Selected.Border" Color="#FF26A0DA"/> 
     <SolidColorBrush x:Key="MenuItem.Selected.Background" Color="#3D26A0DA"/> 
     <Geometry x:Key="Checkmark">F1 M 10.0,1.2 L 4.7,9.1 L 4.5,9.1 L 0,5.2 L 1.3,3.5 L 4.3,6.1L 8.3,0 L 10.0,1.2 Z</Geometry> 
     <SolidColorBrush x:Key="Menu.Static.Foreground" Color="#FF212121"/> 
     <ControlTemplate x:Key="{ComponentResourceKey ResourceId=SubmenuItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}" TargetType="{x:Type MenuItem}"> 
      <Border x:Name="templateRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Height="22" SnapsToDevicePixels="true"> 
       <Grid Margin="0"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="*"/> 
        </Grid.ColumnDefinitions> 
        <ContentPresenter x:Name="menuHeaderContainer" ContentSource="Header" HorizontalAlignment="Stretch" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Stretch"/> 
       </Grid> 
      </Border> 
      <ControlTemplate.Triggers> 
       <Trigger Property="Icon" Value="{x:Null}"/> 
       <Trigger Property="IsChecked" Value="True"/> 
       <Trigger Property="IsHighlighted" Value="True"> 
        <Setter Property="Background" TargetName="templateRoot" Value="{StaticResource MenuItem.Highlight.Background}"/> 
        <Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource MenuItem.Highlight.Border}"/> 
       </Trigger> 
       <Trigger Property="IsEnabled" Value="False"> 
        <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="{StaticResource Menu.Disabled.Foreground}"/> 
       </Trigger> 
       <MultiTrigger> 
        <MultiTrigger.Conditions> 
         <Condition Property="IsHighlighted" Value="True"/> 
         <Condition Property="IsEnabled" Value="False"/> 
        </MultiTrigger.Conditions> 
        <Setter Property="Background" TargetName="templateRoot" Value="{StaticResource MenuItem.Highlight.Disabled.Background}"/> 
        <Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource MenuItem.Highlight.Disabled.Border}"/> 
       </MultiTrigger> 
      </ControlTemplate.Triggers> 
     </ControlTemplate> 
     <SolidColorBrush x:Key="Menu.Static.Border" Color="#FF999999"/> 
     <SolidColorBrush x:Key="Menu.Static.Background" Color="#FFF0F0F0"/> 
     <SolidColorBrush x:Key="Menu.Static.Separator" Color="#FFD7D7D7"/> 
     <Geometry x:Key="UpArrow">M 0,4 L 3.5,0 L 7,4 Z</Geometry> 
     <Style x:Key="MenuScrollButton" BasedOn="{x:Null}" TargetType="{x:Type RepeatButton}"> 
      <Setter Property="ClickMode" Value="Hover"/> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type RepeatButton}"> 
         <Border x:Name="templateRoot" BorderBrush="Transparent" BorderThickness="1" Background="Transparent" SnapsToDevicePixels="true"> 
          <ContentPresenter HorizontalAlignment="Center" Margin="6" VerticalAlignment="Center"/> 
         </Border> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
     <MenuScrollingVisibilityConverter x:Key="MenuScrollingVisibilityConverter"/> 
     <Geometry x:Key="DownArrow">M 0,0 L 3.5,4 L 7,0 Z</Geometry> 
     <Style x:Key="{ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}" BasedOn="{x:Null}" TargetType="{x:Type ScrollViewer}"> 
      <Setter Property="HorizontalScrollBarVisibility" Value="Hidden"/> 
      <Setter Property="VerticalScrollBarVisibility" Value="Auto"/> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type ScrollViewer}"> 
         <Grid SnapsToDevicePixels="true"> 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="*"/> 
          </Grid.ColumnDefinitions> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="Auto"/> 
           <RowDefinition Height="*"/> 
           <RowDefinition Height="Auto"/> 
          </Grid.RowDefinitions> 
          <Border Grid.Column="0" Grid.Row="1"> 
           <ScrollContentPresenter CanContentScroll="{TemplateBinding CanContentScroll}" Margin="{TemplateBinding Padding}"/> 
          </Border> 
          <RepeatButton Grid.Column="0" CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Command="{x:Static ScrollBar.LineUpCommand}" Focusable="false" Grid.Row="0" Style="{StaticResource MenuScrollButton}"> 
           <RepeatButton.Visibility> 
            <MultiBinding ConverterParameter="0" Converter="{StaticResource MenuScrollingVisibilityConverter}" FallbackValue="Visibility.Collapsed"> 
             <Binding Path="ComputedVerticalScrollBarVisibility" RelativeSource="{RelativeSource TemplatedParent}"/> 
             <Binding Path="VerticalOffset" RelativeSource="{RelativeSource TemplatedParent}"/> 
             <Binding Path="ExtentHeight" RelativeSource="{RelativeSource TemplatedParent}"/> 
             <Binding Path="ViewportHeight" RelativeSource="{RelativeSource TemplatedParent}"/> 
            </MultiBinding> 
           </RepeatButton.Visibility> 
           <Path Data="{StaticResource UpArrow}" Fill="{StaticResource Menu.Static.Foreground}"/> 
          </RepeatButton> 
          <RepeatButton Grid.Column="0" CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Command="{x:Static ScrollBar.LineDownCommand}" Focusable="false" Grid.Row="2" Style="{StaticResource MenuScrollButton}"> 
           <RepeatButton.Visibility> 
            <MultiBinding ConverterParameter="100" Converter="{StaticResource MenuScrollingVisibilityConverter}" FallbackValue="Visibility.Collapsed"> 
             <Binding Path="ComputedVerticalScrollBarVisibility" RelativeSource="{RelativeSource TemplatedParent}"/> 
             <Binding Path="VerticalOffset" RelativeSource="{RelativeSource TemplatedParent}"/> 
             <Binding Path="ExtentHeight" RelativeSource="{RelativeSource TemplatedParent}"/> 
             <Binding Path="ViewportHeight" RelativeSource="{RelativeSource TemplatedParent}"/> 
            </MultiBinding> 
           </RepeatButton.Visibility> 
           <Path Data="{StaticResource DownArrow}" Fill="{StaticResource Menu.Static.Foreground}"/> 
          </RepeatButton> 
         </Grid> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
     <ControlTemplate x:Key="{ComponentResourceKey ResourceId=TopLevelHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}" TargetType="{x:Type MenuItem}"> 
      <Border x:Name="templateRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true"> 
       <Grid VerticalAlignment="Center"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="Auto"/> 
         <ColumnDefinition Width="Auto"/> 
        </Grid.ColumnDefinitions> 
        <Path x:Name="GlyphPanel" Data="{StaticResource Checkmark}" Fill="{TemplateBinding Foreground}" FlowDirection="LeftToRight" Margin="3" Visibility="Collapsed" VerticalAlignment="Center"/> 
        <ContentPresenter Grid.Column="1" ContentSource="Header" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
        <Popup x:Name="PART_Popup" AllowsTransparency="true" Focusable="false" IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" Placement="Bottom" PlacementTarget="{Binding ElementName=templateRoot}"> 
         <Border x:Name="SubMenuBorder" BorderBrush="{StaticResource Menu.Static.Border}" BorderThickness="1" Background="{StaticResource Menu.Static.Background}" Padding="0"> 
          <ScrollViewer x:Name="SubMenuScrollViewer" Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}"> 
           <Grid RenderOptions.ClearTypeHint="Enabled"> 
            <Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0"> 
             <Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=SubMenuBorder}" Height="{Binding ActualHeight, ElementName=SubMenuBorder}" Width="{Binding ActualWidth, ElementName=SubMenuBorder}"/> 
            </Canvas> 
            <Rectangle Fill="{StaticResource Menu.Static.Separator}" HorizontalAlignment="Left" Margin="0" Width="1"/> 
            <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Cycle" Grid.IsSharedSizeScope="true" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.TabNavigation="Cycle"/> 
           </Grid> 
          </ScrollViewer> 
         </Border> 
        </Popup> 
       </Grid> 
      </Border> 
      <ControlTemplate.Triggers> 
       <Trigger Property="IsSuspendingPopupAnimation" Value="true"> 
        <Setter Property="PopupAnimation" TargetName="PART_Popup" Value="None"/> 
       </Trigger> 
       <Trigger Property="Icon" Value="{x:Null}"/> 
       <Trigger Property="IsChecked" Value="true"> 
        <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/> 
       </Trigger> 
       <Trigger Property="IsHighlighted" Value="True"> 
        <Setter Property="Background" TargetName="templateRoot" Value="{StaticResource MenuItem.Highlight.Background}"/> 
        <Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource MenuItem.Highlight.Border}"/> 
       </Trigger> 
       <Trigger Property="IsEnabled" Value="False"> 
        <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="{StaticResource Menu.Disabled.Foreground}"/> 
        <Setter Property="Fill" TargetName="GlyphPanel" Value="{StaticResource Menu.Disabled.Foreground}"/> 
       </Trigger> 
       <Trigger Property="ScrollViewer.CanContentScroll" SourceName="SubMenuScrollViewer" Value="false"> 
        <Setter Property="Canvas.Top" TargetName="OpaqueRect" Value="{Binding VerticalOffset, ElementName=SubMenuScrollViewer}"/> 
        <Setter Property="Canvas.Left" TargetName="OpaqueRect" Value="{Binding HorizontalOffset, ElementName=SubMenuScrollViewer}"/> 
       </Trigger> 
      </ControlTemplate.Triggers> 
     </ControlTemplate> 
     <ControlTemplate x:Key="{ComponentResourceKey ResourceId=TopLevelItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}" TargetType="{x:Type MenuItem}"> 
      <Border x:Name="templateRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true"> 
       <Grid VerticalAlignment="Center"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="Auto"/> 
         <ColumnDefinition Width="Auto"/> 
        </Grid.ColumnDefinitions> 
        <ContentPresenter x:Name="Icon" ContentSource="Icon" HorizontalAlignment="Center" Height="16" Margin="3" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" Width="16"/> 
        <Path x:Name="GlyphPanel" Data="{StaticResource Checkmark}" Fill="{StaticResource Menu.Static.Foreground}" FlowDirection="LeftToRight" Margin="3" Visibility="Collapsed" VerticalAlignment="Center"/> 
        <ContentPresenter Grid.Column="1" ContentSource="Header" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
       </Grid> 
      </Border> 
      <ControlTemplate.Triggers> 
       <Trigger Property="Icon" Value="{x:Null}"> 
        <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/> 
       </Trigger> 
       <Trigger Property="IsChecked" Value="true"> 
        <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/> 
        <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/> 
       </Trigger> 
       <Trigger Property="IsHighlighted" Value="True"> 
        <Setter Property="Background" TargetName="templateRoot" Value="{StaticResource MenuItem.Highlight.Background}"/> 
        <Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource MenuItem.Highlight.Border}"/> 
       </Trigger> 
       <Trigger Property="IsEnabled" Value="False"> 
        <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="{StaticResource Menu.Disabled.Foreground}"/> 
        <Setter Property="Fill" TargetName="GlyphPanel" Value="{StaticResource Menu.Disabled.Foreground}"/> 
       </Trigger> 
       <MultiTrigger> 
        <MultiTrigger.Conditions> 
         <Condition Property="IsHighlighted" Value="True"/> 
         <Condition Property="IsEnabled" Value="False"/> 
        </MultiTrigger.Conditions> 
        <Setter Property="Background" TargetName="templateRoot" Value="{StaticResource MenuItem.Highlight.Disabled.Background}"/> 
        <Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource MenuItem.Highlight.Disabled.Border}"/> 
       </MultiTrigger> 
      </ControlTemplate.Triggers> 
     </ControlTemplate> 
     <Geometry x:Key="RightArrow">M 0,0 L 4,3.5 L 0,7 Z</Geometry> 
     <ControlTemplate x:Key="{ComponentResourceKey ResourceId=SubmenuHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}" TargetType="{x:Type MenuItem}"> 
      <Border x:Name="templateRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Height="22" SnapsToDevicePixels="true"> 
       <Grid Margin="-1"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition MinWidth="22" SharedSizeGroup="MenuItemIconColumnGroup" Width="Auto"/> 
         <ColumnDefinition Width="13"/> 
         <ColumnDefinition Width="*"/> 
         <ColumnDefinition Width="30"/> 
         <ColumnDefinition SharedSizeGroup="MenuItemIGTColumnGroup" Width="Auto"/> 
         <ColumnDefinition Width="20"/> 
        </Grid.ColumnDefinitions> 
        <ContentPresenter x:Name="Icon" ContentSource="Icon" HorizontalAlignment="Center" Height="16" Margin="3" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" Width="16"/> 
        <Border x:Name="GlyphPanel" BorderBrush="{StaticResource MenuItem.Highlight.Border}" BorderThickness="1" Background="{StaticResource MenuItem.Highlight.Background}" Height="22" Margin="-1,0,0,0" Visibility="Hidden" VerticalAlignment="Center" Width="22"> 
         <Path x:Name="Glyph" Data="{DynamicResource Checkmark}" Fill="{StaticResource Menu.Static.Foreground}" FlowDirection="LeftToRight" Height="11" Width="9"/> 
        </Border> 
        <ContentPresenter Grid.Column="2" ContentSource="Header" HorizontalAlignment="Left" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center"/> 
        <TextBlock Grid.Column="4" Margin="{TemplateBinding Padding}" Opacity="0.7" Text="{TemplateBinding InputGestureText}" VerticalAlignment="Center"/> 
        <Path x:Name="RightArrow" Grid.Column="5" Data="{StaticResource RightArrow}" Fill="{StaticResource Menu.Static.Foreground}" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Center"/> 
        <Popup x:Name="PART_Popup" AllowsTransparency="true" Focusable="false" HorizontalOffset="-2" IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" Placement="Right" VerticalOffset="-3"> 
         <Border x:Name="SubMenuBorder" BorderBrush="{StaticResource Menu.Static.Border}" BorderThickness="1" Background="{StaticResource Menu.Static.Background}" Padding="2"> 
          <ScrollViewer x:Name="SubMenuScrollViewer" Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}"> 
           <Grid RenderOptions.ClearTypeHint="Enabled"> 
            <Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0"> 
             <Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=SubMenuBorder}" Height="{Binding ActualHeight, ElementName=SubMenuBorder}" Width="{Binding ActualWidth, ElementName=SubMenuBorder}"/> 
            </Canvas> 
            <Rectangle Fill="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" HorizontalAlignment="Left" Margin="29,2,0,2" Width="1"/> 
            <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Cycle" Grid.IsSharedSizeScope="true" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.TabNavigation="Cycle"/> 
           </Grid> 
          </ScrollViewer> 
         </Border> 
        </Popup> 
       </Grid> 
      </Border> 
      <ControlTemplate.Triggers> 
       <Trigger Property="IsSuspendingPopupAnimation" Value="true"> 
        <Setter Property="PopupAnimation" TargetName="PART_Popup" Value="None"/> 
       </Trigger> 
       <Trigger Property="Icon" Value="{x:Null}"> 
        <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/> 
       </Trigger> 
       <Trigger Property="IsChecked" Value="True"> 
        <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/> 
        <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/> 
       </Trigger> 
       <Trigger Property="IsHighlighted" Value="True"> 
        <Setter Property="Background" TargetName="templateRoot" Value="Transparent"/> 
        <Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource MenuItem.Highlight.Border}"/> 
       </Trigger> 
       <Trigger Property="IsEnabled" Value="False"> 
        <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="{StaticResource Menu.Disabled.Foreground}"/> 
        <Setter Property="Fill" TargetName="Glyph" Value="{StaticResource Menu.Disabled.Foreground}"/> 
        <Setter Property="Fill" TargetName="RightArrow" Value="{StaticResource Menu.Disabled.Foreground}"/> 
       </Trigger> 
       <Trigger Property="ScrollViewer.CanContentScroll" SourceName="SubMenuScrollViewer" Value="false"> 
        <Setter Property="Canvas.Top" TargetName="OpaqueRect" Value="{Binding VerticalOffset, ElementName=SubMenuScrollViewer}"/> 
        <Setter Property="Canvas.Left" TargetName="OpaqueRect" Value="{Binding HorizontalOffset, ElementName=SubMenuScrollViewer}"/> 
       </Trigger> 
      </ControlTemplate.Triggers> 
     </ControlTemplate> 
     <Style x:Key="MenuItemStyle1" TargetType="{x:Type MenuItem}"> 
      <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
      <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
      <Setter Property="Background" Value="Transparent"/> 
      <Setter Property="BorderBrush" Value="Transparent"/> 
      <Setter Property="BorderThickness" Value="1"/> 
      <Setter Property="ScrollViewer.PanningMode" Value="Both"/> 
      <Setter Property="Stylus.IsFlicksEnabled" Value="False"/> 
      <Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=SubmenuItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/> 
      <Style.Triggers> 
       <Trigger Property="Role" Value="TopLevelHeader"> 
        <Setter Property="Background" Value="Transparent"/> 
        <Setter Property="BorderBrush" Value="Transparent"/> 
        <Setter Property="Foreground" Value="{StaticResource Menu.Static.Foreground}"/> 
        <Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=TopLevelHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/> 
        <Setter Property="Padding" Value="6,0"/> 
       </Trigger> 
       <Trigger Property="Role" Value="TopLevelItem"> 
        <Setter Property="Background" Value="{StaticResource Menu.Static.Background}"/> 
        <Setter Property="BorderBrush" Value="{StaticResource Menu.Static.Border}"/> 
        <Setter Property="Foreground" Value="{StaticResource Menu.Static.Foreground}"/> 
        <Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=TopLevelItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/> 
        <Setter Property="Padding" Value="6,0"/> 
       </Trigger> 
       <Trigger Property="Role" Value="SubmenuHeader"> 
        <Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=SubmenuHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </Window.Resources> 
    <Grid> 
     <Menu> 
      <MenuItem Header="File" Style="{DynamicResource MenuItemStyle1}"> 
       <MenuItem Header="Exit" Style="{DynamicResource MenuItemStyle1}"/> 
      </MenuItem> 
     </Menu> 
    </Grid> 
</Window> 
+0

什麼定義了「IsHighlighted」屬性? – 2016-11-20 16:31:28

2

我簡單的方法是在ItemTemplate

<ContextMenu.ItemTemplate> 
    <DataTemplate> 
     <Grid Margin="-20,0,-40,0"><!--this part is important--> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="20"/> 
       <ColumnDefinition Width="1*"/> 
      </Grid.ColumnDefinitions> 
      <TextBlock Grid.Column="0" Text="{Binding Ident}"/> 
      <TextBlock Grid.Column="1" Text="{Binding Description}"/> 
     </Grid> 
    </DataTemplate> 
</ContextMenu.ItemTemplate> 

看到完整的答案here

1

我使用負利潤率爲網格:OP中的空白WPF應用爲例以下正在使用WPF Notifyicon(hardcodet)並使用此代碼刪除菜單的圖標部分:

<Window ...> 
    <Window.Resources> 
     <ItemsPanelTemplate x:Key="MenuTPL"> 
     <StackPanel Margin="-30,0,0,0" Background="White"/> 
     </ItemsPanelTemplate> 
    </Window.Resources> 
    <Grid> 
     ... 
     <ContextMenu> 
     <ContextMenu.Style> 
      <Style TargetType="{x:Type ContextMenu}"> 
       <Setter Property="ItemsPanel" Value="{StaticResource MenuTPL}"/> 
      </Style> 
     </ContextMenu.Style> 
     <MenuItem Header="Exit" Click="Exit_MenuItemClick"/> 
     </ContextMenu> 
     ... 
    </Grid> 
</Window> 
1

要刪除空間並永不使用圖標,必須更改MenuItem.SubmenuItemTemplateKey的模板或MenuItem的模板。 如果你只是需要閱讀垂直線,並繼續使用圖標空間folow此answare。

的Windows與網格有我CustomContextMenu.xaml爲網格資源:

<Window ...> 
    <Grid> 
     <Grid.Resources> 
      <ResourceDictionary Source="CustomContextMenu.xaml"/> 
     </Grid.Resources> 
     <Grid.ContextMenu> 
      <ContextMenu> 
       <MenuItem Header="Menu item 1" > 
        <MenuItem Header="Menu item 2" > 
         <MenuItem.Icon> 
          <Image Source="icon.jpg"/> 
         </MenuItem.Icon> 
        </MenuItem> 
       </MenuItem> 
       <Separator Style="{StaticResource MySeparatorStyle}" /> 
       <MenuItem IsEnabled="False" Header="Menu item 3" /> 
      </ContextMenu> 
     </Grid.ContextMenu> 
     <TextBlock>test</TextBlock> 
    </Grid> 
</Window> 

這裏是我的CustomContextMenu.xaml具有CustomSeparatorStyle模板隔離線延伸到上下文菜單的左邊空白處。還有一個用於隱藏垂直線的ContextMenu模板。

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
     <!-- Outer menu --> 
<Style TargetType="{x:Type ContextMenu}"> 
    <Setter Property="OverridesDefaultStyle" Value="True" /> 
    <Setter Property="MaxWidth" Value="295" /> 
    <Setter Property="SnapsToDevicePixels" Value="True" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ContextMenu}"> 
       <!--Here is where you change the border thickness to zero on the menu--> 
       <Border BorderThickness="1" 
         x:Name="Border" 
         CornerRadius="0" 
         BorderBrush="{TemplateBinding BorderBrush}" 
       Background="#CCCCC7"> 
        <Border.Effect> 
         <DropShadowEffect 
         ShadowDepth="2" Color="Black" Direction="135" Opacity=".8"/> 
        </Border.Effect> 
        <StackPanel ClipToBounds="True" 
           Orientation="Vertical" 
           IsItemsHost="True" /> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsMouseOver" Value="true"> 
         <Setter TargetName="Border" 
           Property="Background" 
           Value="#F7F7F4" /> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
    <!-- Separator --> 
    <Style TargetType="{x:Type Separator}" x:Key="CustomSeparatorStyle"> 
     <Setter Property="Height" Value="1" /> 
     <Setter Property="Margin" Value="-30,5,0,5"/> 
     <Setter Property="Background" Value="#F7F7F4" /> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Separator}"> 
        <Border BorderBrush="#DADAD6" BorderThickness="1" /> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

正確的sude菜單是用上面的代碼創建的。您可以注意到尺寸和陰影的差異。爲了保持原來的菜單的陰影,你必須排除Border.Effect

enter image description here

-1

感謝succesfull想法。對於.NET框架4.5和VS 2012我寫的文本菜單和菜單項相應:

 private const double ICON_SIZE = 32; 

    void ContextMenu_Loaded(object sender, System.Windows.RoutedEventArgs e) 
    { 
     if (_pointerControl.ContextMenu.Template != null) 
     { 
      System.Windows.Shapes.Rectangle r1 = _pointerControl.ContextMenu.Template.FindName("3_T", _pointerControl.ContextMenu) as System.Windows.Shapes.Rectangle; 
      System.Windows.Shapes.Rectangle r2 = _pointerControl.ContextMenu.Template.FindName("4_T", _pointerControl.ContextMenu) as System.Windows.Shapes.Rectangle; 
      System.Windows.Shapes.Rectangle r3 = _pointerControl.ContextMenu.Template.FindName("5_T", _pointerControl.ContextMenu) as System.Windows.Shapes.Rectangle; 
      double width = Math.Max(28, ICON_SIZE+14); 
      r1.Width = width; 
      r2.Margin = new System.Windows.Thickness(width + 1, 2, 0, 2); 
      r3.Margin = new System.Windows.Thickness(width + 2, 2, 0, 2); 

     } 
    } 

    void mi_Loaded(object sender, System.Windows.RoutedEventArgs e) 
    { 
     System.Windows.Controls.MenuItem mi = sender as System.Windows.Controls.MenuItem; 
     if (mi != null && mi.Template != null) 
     { 
      System.Windows.Controls.ContentPresenter cp = mi.Template.FindName("Icon", mi) as System.Windows.Controls.ContentPresenter; 
      cp.Height = ICON_SIZE + 6; 
      cp.Width = ICON_SIZE + 6; 
     } 

    } 
+1

對不起,我發佈了另一個主題... – 2016-11-17 11:21:40

0

Simpal和排序方式低於: 創建ItemsPanelTemplate資源

<ItemsPanelTemplate x:Key="MenuItemPanelTemplate"> 
    <StackPanel Background="White"/> 
</ItemsPanelTemplate> 

添加菜單項下面風格的資源和你完成。

<Style TargetType="{x:Type MenuItem}"> 
    <Setter Property="ItemsPanel" Value="{StaticResource MenuItemPanelTemplate}"/> 
</Style> 

申請同一樣式的文本菜單,你需要創建一個更風格爲以下各項

<Style TargetType="{x:Type ContextMenu}"> 
    <Setter Property="ItemsPanel" Value="{StaticResource MenuItemPanelTemplate}"/> 
</Style> 

也上文中的上下文菜單中,您必須添加

<ContextMenu ItemsSource="{Binding MyItems}" > 
    <ContextMenu.ItemTemplate> 
     <DataTemplate> 
       <TextBlock Margin="-20,0,-40,0" Text="{Binding Name}"/> 
     </DataTemplate> 
    </ContextMenu.ItemTemplate> 
</ContextMenu> 

所以它會覆蓋圖標空間並顯示案例textblock.It是最簡單和最簡單的解決方案。

0

使用RadMenuGroupItem。

RadMenuGroupItem繼承自RadMenuItem類,它用作RadMenuItem下拉列表的容器。換句話說,任何UI元素都可以放置在RadMenuGroupItem中。默認情況下,RadMenuGroupItem的背景顏色爲白色,並且與RadMenuItem不同,不存在具有不同顏色的圖標區域,因此您可以在下拉列表中輕鬆使用不同大小的圖標。除此之外,RadMenuGroupItem還具有顯示在所有組項目頂部的Header屬性。

<telerik:RadMenu VerticalAlignment="Top"> 
<telerik:RadMenuItem Header="Shapes" /> 
<telerik:RadMenuItem Header="Sizes"> 
    <telerik:RadMenuGroupItem Header="Header"> 
     <telerik:RadMenuItem Header="Small" IconTemplate="{StaticResource IconTemplate}" IconColumnWidth="35" Height="35" /> 
     <telerik:RadMenuItem Header="Medium" IconTemplate="{StaticResource IconTemplate}" IconColumnWidth="45" Height="45" /> 
     <telerik:RadMenuItem Header="Large" IconTemplate="{StaticResource IconTemplate}" IconColumnWidth="55" Height="55" /> 
    </telerik:RadMenuGroupItem> 
</telerik:RadMenuItem> 

這是結果:

enter image description here