设置菜单项的子菜单高度的最好方法?菜单项、菜单、高度、方法

由网友(丢了心的自己)分享简介:我现在有一个菜单项(上下文菜单的一部分),其中约35〜菜单项。正因为如此,它会导致子菜单是巨大的。尽管我已滚动的能力,我想,而不必仍在滚动的能力来设置这个子菜单的高度。I currently have a menuitem (part of a context menu), with about ~35 menu i...

我现在有一个菜单项(上下文菜单的一部分),其中约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.

阅读全文

相关推荐

最新文章