如何将 menuItem 添加到在 XAML 中设置了 ItemsSource 和 ItemContainerStyle 的上下文菜单
我有以下 XAML 代码。 ItemsSource 中的内容显示为 MenuItems。
<controls:DropDownButton x:Name="btnOwner"
DockPanel.Dock="Left"
Style="{StaticResource btnStyle}"
HorizontalAlignment="Left"
Visibility="{Binding IsOwnerVisible}">
<controls:DropDownButton.Content>
<ContentControl Width="22"
Height="22"
Style="{StaticResource iconOwner}"/>
</controls:DropDownButton.Content>
<controls:DropDownButton.DropDown>
<ContextMenu HorizontalContentAlignment="Stretch"
ItemsSource="{Binding Owners, Mode = TwoWay, UpdateSourceTrigger=PropertyChanged}"
ItemContainerStyle="{StaticResource OwnerStyle}">
</ContextMenu>
</controls:DropDownButton.DropDown>
如何通过 XAML 将新的 menuItem(例如 SubMenuHeader)添加到此列表?
I have the following XAML code. The contents in the ItemsSource are displayed as MenuItems.
<controls:DropDownButton x:Name="btnOwner"
DockPanel.Dock="Left"
Style="{StaticResource btnStyle}"
HorizontalAlignment="Left"
Visibility="{Binding IsOwnerVisible}">
<controls:DropDownButton.Content>
<ContentControl Width="22"
Height="22"
Style="{StaticResource iconOwner}"/>
</controls:DropDownButton.Content>
<controls:DropDownButton.DropDown>
<ContextMenu HorizontalContentAlignment="Stretch"
ItemsSource="{Binding Owners, Mode = TwoWay, UpdateSourceTrigger=PropertyChanged}"
ItemContainerStyle="{StaticResource OwnerStyle}">
</ContextMenu>
</controls:DropDownButton.DropDown>
How can I add a new menuItem something like a SubMenuHeader via XAML to this List?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它会创造自己。您需要提供
ItemTemplate
来决定在每个MenuItem
中显示什么以及如何显示。否则,默认实现将为Owners
中的每个项目调用ToString()
方法,并将其显示在MenuItem
中。在这里,我假设所有者的类型有一个属性名称
Title
。例如,如果Owners
是ObservableCollection
,则Owner
定义为:这是如何使用
的基本思想项目模板
。现在,如果您想要上下文菜单中的子菜单项,那么您必须使用HierarchicalDataTemplate
而不是ItemTemplate
定义中的DataTemplate
。It will create itself. All that you need to provide the
ItemTemplate
in which you will decide what to show and how to show in eachMenuItem
. Otherwise, the default implemention will callToString()
method for each item inOwners
, and will display it inMenuItem
.Here, I assumed that the type of owner has a property name
Title
. For example, ifOwners
isObservableCollection<Owner>
, thenOwner
is defined as:That is basic idea as to how to use
ItemTemplate
. Now if you want submenuitem in the context menu, then you've to useHierarchicalDataTemplate
instead ofDataTemplate
in theItemTemplate
definition.