设置MenuItem的子菜单高度的最佳方法是什么?

时间:2021-06-21 03:14:59

I currently have a menuitem (part of a context menu), with about ~35 menu items. Because of this, it causes the sub-menu to be huge. Even though I have scrolling ability, I would like to set the height of this submenu while having the ability to still scroll.

我目前有一个menuitem(上下文菜单的一部分),大约有35个菜单项。因此,它会导致子菜单变得庞大。虽然我有滚动功能,但我想设置这个子菜单的高度,同时还能够滚动。

I've messed w/ the MenuItem.Itemspanel but have not been able to set the sub-menu's height and still scroll.

我已经弄乱了MenuItem.Itemspanel但是无法设置子菜单的高度并且仍然滚动。

1 个解决方案

#1


1  

Unfortunately, WPF does not allow this to be modified by properties. You will have to modify the default ControlTemplate.

不幸的是,WPF不允许通过属性修改它。您必须修改默认的ControlTemplate。

EDIT: I revised the blog entry on that here

编辑:我在这里修改了博客条目

Here is a sample (notice the addition of "MaxHeight" on "SubMenuScrollViewer"):

这是一个示例(注意在“SubMenuScrollViewer”上添加“MaxHeight”):

<Popup x:Name="PART_Popup"
    AllowsTransparency="true"
    Placement="Right"
    VerticalOffset="-3"
    HorizontalOffset="-2"
    IsOpen="{Binding Path=IsSubmenuOpen,RelativeSource={RelativeSource TemplatedParent}}"
    Focusable="false"
    PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}">
    <theme:SystemDropShadowChrome Name="Shdw" Color="Transparent">
        <ContentControl Name="SubMenuBorder"
            Template="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type FrameworkElement}, ResourceId=SubmenuContent}}"
            IsTabStop="false">
            <ScrollViewer Name="SubMenuScrollViewer" CanContentScroll="true" MaxHeight="400" Style="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type FrameworkElement}, ResourceId=MenuScrollViewer}}">
                <Grid RenderOptions.ClearTypeHint="Enabled">
                    <Canvas Height="0" Width="0" HorizontalAlignment="Left" VerticalAlignment="Top">
                        <Rectangle
                            Height="{Binding ElementName=SubMenuBorder,Path=ActualHeight}" 
                            Width="{Binding ElementName=SubMenuBorder,Path=ActualWidth}" 
                            Fill="{StaticResource SubMenuBackgroundBrush}" />
                    </Canvas>
                    <ItemsPresenter Name="ItemsPresenter" Margin="2"
                        KeyboardNavigation.TabNavigation="Cycle"
                        KeyboardNavigation.DirectionalNavigation="Cycle"
                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                        Grid.IsSharedSizeScope="true"/>
                </Grid>
            </ScrollViewer>
        </ContentControl>
    </theme:SystemDropShadowChrome>
</Popup>

This is just to override the Aero theme. As you can see there is a lot of XAML for the MenuItem, so it's not graceful. Just make a separate ResourceDictionary to keep things tidy.

这只是为了覆盖Aero主题。正如您所看到的,MenuItem有很多XAML,所以它并不优雅。只需创建一个单独的ResourceDictionary即可保持整洁。

#1


1  

Unfortunately, WPF does not allow this to be modified by properties. You will have to modify the default ControlTemplate.

不幸的是,WPF不允许通过属性修改它。您必须修改默认的ControlTemplate。

EDIT: I revised the blog entry on that here

编辑:我在这里修改了博客条目

Here is a sample (notice the addition of "MaxHeight" on "SubMenuScrollViewer"):

这是一个示例(注意在“SubMenuScrollViewer”上添加“MaxHeight”):

<Popup x:Name="PART_Popup"
    AllowsTransparency="true"
    Placement="Right"
    VerticalOffset="-3"
    HorizontalOffset="-2"
    IsOpen="{Binding Path=IsSubmenuOpen,RelativeSource={RelativeSource TemplatedParent}}"
    Focusable="false"
    PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}">
    <theme:SystemDropShadowChrome Name="Shdw" Color="Transparent">
        <ContentControl Name="SubMenuBorder"
            Template="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type FrameworkElement}, ResourceId=SubmenuContent}}"
            IsTabStop="false">
            <ScrollViewer Name="SubMenuScrollViewer" CanContentScroll="true" MaxHeight="400" Style="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type FrameworkElement}, ResourceId=MenuScrollViewer}}">
                <Grid RenderOptions.ClearTypeHint="Enabled">
                    <Canvas Height="0" Width="0" HorizontalAlignment="Left" VerticalAlignment="Top">
                        <Rectangle
                            Height="{Binding ElementName=SubMenuBorder,Path=ActualHeight}" 
                            Width="{Binding ElementName=SubMenuBorder,Path=ActualWidth}" 
                            Fill="{StaticResource SubMenuBackgroundBrush}" />
                    </Canvas>
                    <ItemsPresenter Name="ItemsPresenter" Margin="2"
                        KeyboardNavigation.TabNavigation="Cycle"
                        KeyboardNavigation.DirectionalNavigation="Cycle"
                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                        Grid.IsSharedSizeScope="true"/>
                </Grid>
            </ScrollViewer>
        </ContentControl>
    </theme:SystemDropShadowChrome>
</Popup>

This is just to override the Aero theme. As you can see there is a lot of XAML for the MenuItem, so it's not graceful. Just make a separate ResourceDictionary to keep things tidy.

这只是为了覆盖Aero主题。正如您所看到的,MenuItem有很多XAML,所以它并不优雅。只需创建一个单独的ResourceDictionary即可保持整洁。