【问题标题】:How can I remove the Menu Item white border?如何删除菜单项白色边框?
【发布时间】:2021-04-07 19:11:55
【问题描述】:

我说的是这个。

我尝试过使用模板,但无法弄清楚。 到目前为止,这是我的 XAML。

<Border BorderThickness="0" HorizontalAlignment="Left" Height="30" Margin="3,30,0,0" Background="#242424" VerticalAlignment="Top" Width="660">
    <Menu HorizontalAlignment="Left" Height="27" VerticalAlignment="Top" FontFamily="Segoe UI" Width="645" Background="#FF242424" Margin="10,3,0,0">
        <MenuItem Header="File" FontSize="14" Padding="2" Foreground="White">
            <MenuItem Header="Save" BorderThickness="0" Background="#2d2d2d" Foreground="White"/>
            <MenuItem Header="Open" BorderThickness="0" Background="#2d2d2d" Foreground="White"/>
        </MenuItem>
    </Menu>
</Border>

【问题讨论】:

    标签: wpf xaml controltemplate


    【解决方案1】:

    为了移除边框,你必须编辑包含Border的控件模板。

    <Border x:Name="SubMenuBorder" Background="{StaticResource Menu.Static.Background}" BorderThickness="0" BorderBrush="{StaticResource Menu.Static.Border}" Padding="2">
    

    您注意到的边框实际上是由硬编码的BorderThicknessPadding引起的。

    <Border x:Name="SubMenuBorder" Background="{StaticResource Menu.Static.Background}">
    

    如果您以MenuItem 的默认样式删除顶级标题模板子菜单标题模板(参见下面的代码),则可见边框将消失如下所示。

    您可以提取MenuItem using Blend or Visual Studio 的默认样式和模板。然后你只需要调整上面的行。这是样式和模板的适应 XAML 代码:

    <SolidColorBrush x:Key="Menu.Static.Background" Color="#FFF0F0F0"/>
    <SolidColorBrush x:Key="Menu.Static.Border" Color="#FF999999"/>
    <SolidColorBrush x:Key="Menu.Static.Foreground" Color="#FF212121"/>
    <SolidColorBrush x:Key="Menu.Static.Separator" Color="#FFD7D7D7"/>
    <SolidColorBrush x:Key="Menu.Disabled.Foreground" Color="#FF707070"/>
    <SolidColorBrush x:Key="MenuItem.Selected.Background" Color="#3D26A0DA"/>
    <SolidColorBrush x:Key="MenuItem.Selected.Border" Color="#FF26A0DA"/>
    <SolidColorBrush x:Key="MenuItem.Highlight.Background" Color="#3D26A0DA"/>
    <SolidColorBrush x:Key="MenuItem.Highlight.Border" Color="#FF26A0DA"/>
    <SolidColorBrush x:Key="MenuItem.Highlight.Disabled.Background" Color="#0A000000"/>
    <SolidColorBrush x:Key="MenuItem.Highlight.Disabled.Border" Color="#21000000"/>
    <MenuScrollingVisibilityConverter x:Key="MenuScrollingVisibilityConverter"/>
    <Geometry x:Key="DownArrow">M 0,0 L 3.5,4 L 7,0 Z</Geometry>
    <Geometry x:Key="UpArrow">M 0,4 L 3.5,0 L 7,4 Z</Geometry>
    <Geometry x:Key="RightArrow">M 0,0 L 4,3.5 L 0,7 Z</Geometry>
    <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>
    <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" Background="Transparent" BorderThickness="1" BorderBrush="Transparent" SnapsToDevicePixels="true">
                   <ContentPresenter HorizontalAlignment="Center" Margin="6" VerticalAlignment="Center"/>
                </Border>
             </ControlTemplate>
          </Setter.Value>
       </Setter>
    </Style>
    <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 Command="{x:Static ScrollBar.LineUpCommand}" CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Grid.Column="0" Focusable="false" Grid.Row="0" Style="{StaticResource MenuScrollButton}">
                      <RepeatButton.Visibility>
                         <MultiBinding Converter="{StaticResource MenuScrollingVisibilityConverter}" ConverterParameter="0" 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 Command="{x:Static ScrollBar.LineDownCommand}" CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Grid.Column="0" Focusable="false" Grid.Row="2" Style="{StaticResource MenuScrollButton}">
                      <RepeatButton.Visibility>
                         <MultiBinding Converter="{StaticResource MenuScrollingVisibilityConverter}" ConverterParameter="100" 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=TopLevelItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}" TargetType="{x:Type MenuItem}">
       <Border x:Name="templateRoot" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" 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}" FlowDirection="LeftToRight" Fill="{StaticResource Menu.Static.Foreground}" Margin="3" VerticalAlignment="Center" Visibility="Collapsed"/>
             <ContentPresenter ContentSource="Header" Grid.Column="1" 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>
    <ControlTemplate x:Key="{ComponentResourceKey ResourceId=TopLevelHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}" TargetType="{x:Type MenuItem}">
       <Border x:Name="templateRoot" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" 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}" FlowDirection="LeftToRight" Fill="{TemplateBinding Foreground}" Margin="3" VerticalAlignment="Center" Visibility="Collapsed"/>
             <ContentPresenter ContentSource="Header" Grid.Column="1" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
             <Popup x:Name="PART_Popup" AllowsTransparency="true" Focusable="false" IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" Placement="Bottom" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" PlacementTarget="{Binding ElementName=templateRoot}">
                <Border x:Name="SubMenuBorder" Background="{StaticResource Menu.Static.Background}">
                   <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="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="{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=SubmenuItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}" TargetType="{x:Type MenuItem}">
       <Border x:Name="templateRoot" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" 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" Background="{StaticResource MenuItem.Selected.Background}" BorderThickness="1" BorderBrush="{StaticResource MenuItem.Selected.Border}" ClipToBounds="False" HorizontalAlignment="Center" Height="22" Margin="-1,0,0,0" VerticalAlignment="Center" Visibility="Hidden" Width="22">
                <Path x:Name="Glyph" Data="{StaticResource Checkmark}" FlowDirection="LeftToRight" Fill="{StaticResource Menu.Static.Foreground}" Height="11" Width="10"/>
             </Border>
             <ContentPresenter x:Name="menuHeaderContainer" ContentSource="Header" Grid.Column="2" HorizontalAlignment="Left" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center"/>
             <TextBlock x:Name="menuGestureText" Grid.Column="4" Margin="{TemplateBinding Padding}" Opacity="0.7" Text="{TemplateBinding InputGestureText}" VerticalAlignment="Center"/>
          </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="Glyph" 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>
    <ControlTemplate x:Key="{ComponentResourceKey ResourceId=SubmenuHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}" TargetType="{x:Type MenuItem}">
       <Border x:Name="templateRoot" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" 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" Background="{StaticResource MenuItem.Highlight.Background}" BorderThickness="1" BorderBrush="{StaticResource MenuItem.Highlight.Border}" Height="22" Margin="-1,0,0,0" VerticalAlignment="Center" Visibility="Hidden" Width="22">
                <Path x:Name="Glyph" Data="{DynamicResource Checkmark}" FlowDirection="LeftToRight" Fill="{StaticResource Menu.Static.Foreground}" Height="11" Width="9"/>
             </Border>
             <ContentPresenter ContentSource="Header" Grid.Column="2" 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}}" Placement="Right" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" VerticalOffset="-3">
                <Border x:Name="SubMenuBorder" Background="{StaticResource Menu.Static.Background}">
                   <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 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>MenuItem
          <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>
    

    将代码复制到范围内的资源字典中。 MenuItem 的样式是隐式的,因此它会自动应用于范围内的所有 MenuItems。如果您只想有选择地应用此样式,请添加 x:Key 以使其明确并通过 StaticResourceDynamicResource 标记扩展来引用它。

    当然,您可以使用 documentation 中关于 MenuItem 样式和模板的指导来滚动您自己的自定义菜单项样式,其中包含所需的状态和部分,但这是一项复杂的任务并且容易出错,因此更容易复制和调整默认样式。

    【讨论】:

      【解决方案2】:

      边框在Menu 控件的ControlTemplate 中声明。请参阅official documentation 以应用自定义样式。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-09
        • 2015-08-03
        相关资源
        最近更新 更多