首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >设置MenuItem的子菜单高度的最佳方法?

设置MenuItem的子菜单高度的最佳方法?
EN

Stack Overflow用户
提问于 2011-12-16 06:35:57
回答 1查看 1.9K关注 0票数 1

我目前有一个菜单项(上下文菜单的一部分),大约有35个菜单项。正因为如此,它导致子菜单变得很大。即使我有滚动的能力,我想设置这个子菜单的高度,同时仍然有滚动的能力。

我已经把MenuItem.Itemspanel弄乱了,但是还不能设置子菜单的高度和滚动。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-18 01:30:02

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

编辑:我修改了该here上的博客条目

下面是一个示例(注意在“SubMenuScrollViewer”上添加了"MaxHeight“):

代码语言:javascript
复制
<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主题。正如您所看到的,有很多用于MenuItem的XAML,所以它并不美观。只需创建一个单独的ResourceDictionary来保持整洁即可。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8527612

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档