我想为菜单创建一个样式。如果有更多X元素,则菜单中应显示滚动条(滚动所有菜单项)。 目前我的风格是:
<MenuItem.Resources>
<!--From BFR, need to retrieve from SDK-->
<Style x:Key="HierarchyDynamicMenuStyle" TargetType="{x:Type MenuItem}" BasedOn="{StaticResource {x:Type MenuItem}}">
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource {x:Type MenuItem}}">
<Setter Property="Header" Value="{Binding Path=Header}" />
<Setter Property="Command" Value="{Binding Command}"/>
<Setter Property="CommandParameter" Value="{Binding Path=CommandParameter}"/>
<Setter Property="ItemsSource" Value="{Binding Children}"/>
</Style>
</Setter.Value>
</Setter>
<Setter Property="Template" >
<Setter.Value>
<ControlTemplate>
<ScrollViewer>
<ContentPresenter/>
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</MenuItem.Resources>
现在我添加了一个新菜单:
<MenuItem Header="My menu with scrollbar" Style="{StaticResource HierarchyDynamicMenuStyle}" ItemsSource="{Binding MyItems}"></MenuItem>
你能帮我一个模板吗?滚动条或项目计数等可能存在一些高度限制。
答案 0 :(得分:1)
如果某人看到同一个问题的例子:
<MenuItem.Resources>
<Style x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type FrameworkElement}, ResourceId=MenuScrollViewer}"
TargetType="{x:Type ScrollViewer}" BasedOn="{StaticResource {ComponentResourceKey TypeInTargetAssembly={x:Type FrameworkElement}, ResourceId=MenuScrollViewer}}">
<Setter Property="MaxHeight" Value="480"/>
</Style>
<!--From BFR, need to retrieve from SDK-->
<Style x:Key="HierarchicalDynamicMenuStyle" TargetType="{x:Type MenuItem}" BasedOn="{StaticResource {x:Type MenuItem}}">
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource {x:Type MenuItem}}">
<Setter Property="Header" Value="{Binding Path=Header}" />
<Setter Property="Command" Value="{Binding Command}"/>
<Setter Property="CommandParameter" Value="{Binding Path=CommandParameter}"/>
<Setter Property="ItemsSource" Value="{Binding Children}"/>
</Style>
</Setter.Value>
</Setter>
</Style>
</MenuItem.Resources>
其中 480 是可滚动区域的最大高度
这是层次结构菜单节点的示例:
public class HierarchyMenuItem : NotificationObject
{
public ICommand Command { get; set; }
public string Header { get; set; }
public string CommandParameter { get; set; }
public ObservableCollection<HierarchyMenuItem> Children { get; set; }
}
答案 1 :(得分:0)
<Setter Property="Template" >
<Setter.Value>
<ControlTemplate>
<ScrollViewer>
<ContentPresenter/>
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
答案 2 :(得分:0)
看看这个。 您必须更改菜单控件'ItemsPanel(我认为,它是wrapPanel)
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Menu>
<MenuItem Header="Hello"></MenuItem>
<MenuItem Header="Bye"></MenuItem>
<MenuItem Header="Alex"></MenuItem>
<MenuItem Header="Alex"></MenuItem>
<MenuItem Header="Alex"></MenuItem>
<MenuItem Header="Alex"></MenuItem>
<MenuItem Header="Alex"></MenuItem>
<MenuItem Header="Alex"></MenuItem>
<MenuItem Header="Alex"></MenuItem>
<MenuItem Header="Alex"></MenuItem>
<MenuItem Header="Alex"></MenuItem>
<MenuItem Header="Alex"></MenuItem>
<MenuItem Header="Alex"></MenuItem>
<MenuItem Header="Alex"></MenuItem>
<MenuItem Header="Alex"></MenuItem>
<MenuItem Header="Alex"></MenuItem>
<MenuItem Header="John"></MenuItem>
<Menu.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</Menu.ItemsPanel>
<Menu.Template>
<ControlTemplate>
<ScrollViewer HorizontalScrollBarVisibility="Auto" x:Name="ScrollViewer" >
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
</Menu.Template>
</Menu>
</Grid>
</Window>