我目前有一个菜单项(上下文菜单的一部分),大约有35个菜单项。正因为如此,它导致子菜单变得很大。即使我有滚动的能力,我想设置这个子菜单的高度,同时仍然有滚动的能力。
我已经把MenuItem.Itemspanel弄乱了,但是还不能设置子菜单的高度和滚动。
发布于 2011-12-18 01:30:02
不幸的是,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来保持整洁即可。
https://stackoverflow.com/questions/8527612
复制相似问题