【问题标题】:Best way to set a MenuItem's sub-menu height?设置 MenuItem 的子菜单高度的最佳方法?
【发布时间】:2015-06-05 21:02:01
【问题描述】:

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

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

【问题讨论】:

  • 不是答案,但我认为您在这里有一些设计问题。 35 个项目对于上下文菜单来说太多了。你见过滚动的上下文菜单吗? =) 您可以将项目划分为子菜单,或者更好地制作普通菜单或工具栏。
  • 它并没有否认它应该是可能的。 Windows 做到了。
  • @Xcalibur37,Windows 可以吗?您的意思是某些 Microsoft 应用程序具有滚动的 context 菜单?这其实很有趣,你能举个例子吗?
  • 我说的是开始菜单滚动。上下文菜单仍然是一个菜单对象。你在 Windows XP 之前就看到过这个功能(虽然老实说我大部分时间都关闭了它)。

标签: .net wpf xaml contextmenu menuitem


【解决方案1】:

很遗憾,WPF 不允许通过属性对其进行修改。您必须修改默认的 ControlTemplate。

编辑: 我修改了那个here的博客条目

这是一个示例(注意在“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>

这只是为了覆盖 Aero 主题。如您所见,MenuItem 有很多 XAML,因此它并不优雅。只需制作一个单独的 ResourceDictionary 以保持整洁。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-07
  • 2014-03-18
  • 1970-01-01
  • 2017-08-08
  • 2011-02-04
相关资源
最近更新 更多