Best way to set a MenuItem's sub-menu height?

前端 未结 1 1736
故里飘歌
故里飘歌 2021-01-25 01:46

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 woul

相关标签:
1条回答
  • 2021-01-25 02:07

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

    EDIT: I revised the blog entry on that here

    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>
    

    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.

    0 讨论(0)
提交回复
热议问题