设置菜单项的子菜单高度的最好方法? [英] Best way to set a MenuItem's sub-menu height?

查看:332
本文介绍了设置菜单项的子菜单高度的最好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个菜单项(上下文菜单的一部分),其中约35〜菜单项。正因为如此,它会导致子菜单是巨大的。尽管我已滚动的能力,我想,而不必仍在滚动的能力来设置这个子菜单的高度。

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.Itemspanel但一直没能设置子菜单的高度,仍然滚动。

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

推荐答案

不幸的是,WPF不允许这种由物业进行修改。你将不得不修改默认的ControlTemplate。

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

编辑: 我在这里修订上博客条目

下面是一个示例(注意添加对SubMenuScrollViewer了maxHeight的):

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

<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>

这仅仅是覆盖了Aero主题。正如你可以看到有很多的XA​​ML的菜单项的,所以它不是优雅。只是做一个单独的ResourceDictionary,以保持整洁。

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.

这篇关于设置菜单项的子菜单高度的最好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆