Пробелы до и после MenuItems в меню - PullRequest
1 голос
/ 22 января 2020

Я реализовал Меню в строке заголовка с использованием Mahapps metroWindow. Однако, когда я нажимаю на MenuItem, чтобы открыть раскрывающийся список с другими MenuItems, возникает странное расстояние до и после этих элементов. Могу ли я как-то от этого избавиться?

Described problem

Этот код стоит за ним:

<Controls:MetroWindow.LeftWindowCommands>
    <Controls:WindowCommands >
        <StackPanel Name="menuHolder" Orientation="Horizontal" Height="30">
            <Menu Name="mnuMain" Background="orange" Height="30" Padding="0">
                <MenuItem Name="File" Height="30" Header="File">
                    <MenuItem Name="Open" Header="Open" Background="{StaticResource DefaultContent}" Margin="0"/>
                    <MenuItem Name="Close" Header="Close" Background="{StaticResource DefaultContent}"/>
                </MenuItem>
                <MenuItem Name="Edit" Height="30" Header="Edit">
                    <MenuItem Name="Copy" Header="Copy" Background="{StaticResource DefaultContent}" />
                    <MenuItem Name="Paste" Header="Paste" Background="{StaticResource DefaultContent}"/>
                </MenuItem>
            </Menu>
        </StackPanel>
    </Controls:WindowCommands>
</Controls:MetroWindow.LeftWindowCommands>

Любая помощь оценена.

1 Ответ

1 голос
/ 22 января 2020

Вам необходимо отредактировать ControlTemplate для MenuItem.

Скопируйте его из GitHub и избавьтесь от полей элемента Grid в ScrollViewer в Popup:

<Menu Name="mnuMain" Background="orange" Height="30" Padding="0">
    <Menu.Resources>
        <Style TargetType="{x:Type MenuItem}" BasedOn="{x:Null}">
            <Setter Property="Padding" Value="24 0 24 0"></Setter>
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type MenuItem}">
                        <ControlTemplate.Resources>
                            <Style TargetType="{x:Type materialDesign:PackIcon}" >
                                <Setter Property="Width" Value="16" />
                                <Setter Property="Height" Value="16" />
                            </Style>
                        </ControlTemplate.Resources>
                        <Grid ClipToBounds="True">
                            <Border x:Name="templateRoot"
                                                BorderBrush="{TemplateBinding BorderBrush}"
                                                BorderThickness="{TemplateBinding BorderThickness}"
                                                Background="{TemplateBinding Background}"
                                                SnapsToDevicePixels="True"/>
                            <Border x:Name="BackgroundRoot"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Background="{TemplateBinding Foreground, Converter={StaticResource BrushRoundConverter}}"
                                SnapsToDevicePixels="True"
                                Opacity="0"/>
                            <materialDesign:Ripple Background="Transparent"
                                        HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"
                                        Focusable="False"
                                        Feedback="{TemplateBinding Foreground, Converter={StaticResource BrushRoundConverter}}">
                                <Grid Height="{TemplateBinding Height}" Background="Transparent">
                                    <Grid VerticalAlignment="Center"
                                          Background="Transparent"
                                          Margin="{TemplateBinding Padding}">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto" SharedSizeGroup="IconGroup"/>
                                            <ColumnDefinition Width="*" SharedSizeGroup="HeaderGroup"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <Grid x:Name="IconWrapper" Visibility="Visible" Width="40">
                                            <ContentPresenter x:Name="Icon"
                                                              Content="{TemplateBinding Icon}"
                                                              ContentSource="Icon"
                                                              HorizontalAlignment="Left" VerticalAlignment="Center"
                                                              Height="16" Width="16"
                                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                        </Grid>
                                        <Grid x:Name="GlyphWrapper" Visibility="Collapsed" Width="40">
                                            <Viewbox x:Name="GlyphPanel"
                                                     Width="16" Height="16"
                                                     HorizontalAlignment="Left"
                                                     Margin="0" Visibility="Collapsed" VerticalAlignment="Center"
                                                     FlowDirection="LeftToRight">
                                                <Canvas Width="24" Height="24">
                                                    <Path Data="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z" 
                                                          x:Name="Glyph"
                                                          Fill="{TemplateBinding Foreground}" FlowDirection="LeftToRight" />
                                                </Canvas>
                                            </Viewbox>
                                        </Grid>
                                        <Grid Grid.Column="1">
                                            <ContentPresenter x:Name="BoldHeaderPresenter"
                                                          Visibility="Hidden"
                                                          TextBlock.FontWeight="Bold"
                                                          ContentTemplate="{TemplateBinding HeaderTemplate}"
                                                          Content="{TemplateBinding Header}"
                                                          ContentStringFormat="{TemplateBinding HeaderStringFormat}"
                                                          ContentSource="Header"
                                                          RecognizesAccessKey="True"
                                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                                          VerticalAlignment="Center"/>
                                            <ContentPresenter x:Name="HeaderPresenter"
                                                          ContentTemplate="{TemplateBinding HeaderTemplate}"
                                                          Content="{TemplateBinding Header}"
                                                          ContentStringFormat="{TemplateBinding HeaderStringFormat}"
                                                          ContentSource="Header"
                                                          RecognizesAccessKey="True"
                                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                                          VerticalAlignment="Center"/>
                                        </Grid>
                                        <Grid x:Name="InputGestureTextWrapper"
                                              Grid.Column="2"
                                              Visibility="Collapsed">
                                            <TextBlock Visibility="{TemplateBinding InputGestureText, Converter={StaticResource StringIsEmptyVisibilityConverter}}"
                                                       Margin="16 0 0 0"
                                                       VerticalAlignment="Center" HorizontalAlignment="Right"
                                                       Text="{TemplateBinding InputGestureText}"
                                                       SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                                       Foreground="{TemplateBinding Foreground}"/>
                                        </Grid>
                                        <Grid x:Name="SubBlock"
                                              Grid.Column="2"
                                              Visibility="Collapsed"
                                              Margin="16 0 0 0">
                                            <Path Width="5" Height="10"
                                                  Stretch="Uniform"
                                                  HorizontalAlignment="Right"
                                                  VerticalAlignment="Center"
                                                  Data="M0,16 L8,8 L0,0 Z"
                                                  Fill="{TemplateBinding Foreground}"
                                                  SnapsToDevicePixels="False"/>
                                        </Grid>
                                    </Grid>
                                </Grid>
                            </materialDesign:Ripple>
                            <Popup x:Name="PART_Popup"
                               AllowsTransparency="True"
                               Focusable="False"
                               IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}"
                               PopupAnimation="Slide"
                               Placement="Bottom"
                                CacheMode="{Binding RelativeSource={RelativeSource Self}, Path=(materialDesign:ShadowAssist.CacheMode)}">
                                <Border x:Name="SubMenuBorder"
                                    Background="{Binding Path=Background, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=MenuBase}}"
                                    Effect="{DynamicResource MaterialDesignShadowDepth1}"
                                    CornerRadius="2">

                                    <Border Background="Transparent">
                                        <ScrollViewer x:Name="SubMenuScrollViewer" Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}">
                                            <Grid RenderOptions.ClearTypeHint="Enabled" Margin="0">
                                                <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>
                                                <ItemsPresenter x:Name="ItemsPresenter"
                                                            KeyboardNavigation.DirectionalNavigation="Cycle"
                                                            Grid.IsSharedSizeScope="True"
                                                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                                            KeyboardNavigation.TabNavigation="Cycle"/>
                                            </Grid>
                                        </ScrollViewer>
                                    </Border>
                                </Border>
                            </Popup>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="Role" Value="SubmenuHeader">
                                <Setter TargetName="SubBlock" Property="Visibility" Value="Visible" />
                                <Setter TargetName="PART_Popup" Property="Placement" Value="Right" />
                                <Setter Property="Height" Value="32"/>
                                <Setter TargetName="BoldHeaderPresenter" Property="Visibility" Value="Collapsed"/>
                            </Trigger>
                            <Trigger Property="Role" Value="SubmenuItem">
                                <Setter Property="Height" Value="32"/>
                                <Setter TargetName="InputGestureTextWrapper" Property="Visibility" Value="Visible"/>
                                <Setter TargetName="BoldHeaderPresenter" Property="Visibility" Value="Collapsed"/>
                            </Trigger>
                            <Trigger Property="Role" Value="TopLevelHeader">
                                <Setter Property="Grid.IsSharedSizeScope" Value="True"/>
                                <Setter Property="Padding" Value="16 0"/>
                                <Setter Property="Height" Value="48"/>
                                <Setter TargetName="templateRoot" Property="CornerRadius" Value="2"/>
                                <Setter TargetName="BackgroundRoot" Property="CornerRadius" Value="2"/>
                            </Trigger>
                            <Trigger Property="Role" Value="TopLevelItem">
                                <Setter Property="Padding" Value="16 0"/>
                                <Setter Property="Height" Value="48"/>
                                <Setter TargetName="templateRoot" Property="CornerRadius" Value="2"/>
                                <Setter TargetName="BackgroundRoot" Property="CornerRadius" Value="2"/>
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="Role" Value="TopLevelHeader"/>
                                    <Condition Property="IsSubmenuOpen" Value="True"/>
                                </MultiTrigger.Conditions>
                                <MultiTrigger.Setters>
                                    <Setter TargetName="HeaderPresenter" Property="TextBlock.FontWeight" Value="Bold"/>
                                </MultiTrigger.Setters>
                            </MultiTrigger>

                            <Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="True">
                                <Setter TargetName="SubMenuBorder" Property="Margin" Value="5"/>
                            </Trigger>

                            <Trigger Property="IsSuspendingPopupAnimation" Value="True">
                                <Setter Property="PopupAnimation" TargetName="PART_Popup" Value="None"/>
                            </Trigger>
                            <Trigger Property="Icon" Value="{x:Null}">
                                <Setter Property="Visibility" TargetName="IconWrapper" Value="Collapsed"/>
                            </Trigger>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/>
                            </Trigger>
                            <Trigger Property="IsCheckable" Value="True">
                                <Setter Property="Visibility" TargetName="GlyphWrapper" Value="Visible"/>
                                <Setter Property="Visibility" TargetName="IconWrapper" Value="Collapsed"/>
                            </Trigger>
                            <Trigger Property="IsHighlighted" Value="True">
                                <Setter TargetName="BackgroundRoot" Property="Opacity" Value="0.13"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Opacity" Value=".56"/>
                            </Trigger>
                            <Trigger Property="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>
                </Setter.Value>
            </Setter>
        </Style>

    </Menu.Resources>
    <MenuItem Name="File" Height="30" Header="File">
        <MenuItem Name="Open" Header="Open" Background="{StaticResource DefaultContent}" Margin="0"/>
        <MenuItem Name="Close" Header="Close" Background="{StaticResource DefaultContent}"/>
    </MenuItem>
    <MenuItem Name="Edit" Height="30" Header="Edit">
        <MenuItem Name="Copy" Header="Copy" Background="{StaticResource DefaultContent}" />
        <MenuItem Name="Paste" Header="Paste" Background="{StaticResource DefaultContent}"/>
    </MenuItem>
</Menu>
...