如何添加水平分隔在动态创建的文本菜单? [英] How to add horizontal separator in a dynamically created ContextMenu?
问题描述
我一直在寻找互联网上的解决方案,但未能我的样本中找到它。我需要补充的是从code产生的背后上下文菜单项之间的分隔符。我想这样的code线如下图所示但没有成功添加。
this.Commands.Add(新ToolStripSeparator());
我想知道,如果有人可以提供帮助。谢谢你在前进。
右键菜单XAML:
<风格X:键=DataGridCellStyle的TargetType ={X:类型DataGridCell}>
< setter属性=文本菜单>
< Setter.Value>
<文本菜单的ItemsSource ={绑定命令}>
< ContextMenu.ItemContainerStyle>
<风格的TargetType ={X:类型菜单项}>
< setter属性=命令VALUE ={结合}/>
< setter属性=标题VALUE ={绑定路径=文本}/>
< setter属性=CommandParameterVALUE ={绑定路径=参数}/>
< /样式和GT;
< /ContextMenu.ItemContainerStyle>
< /文本菜单>
< /Setter.Value>
< /二传手>
,在该方法中加入#:
this.Commands =新的ObservableCollection<&的ICommand GT;();
this.Commands.Add(MainWindow.AddRole1);
this.Commands.Add(MainWindow.AddRole2);
this.Commands.Add(MainWindow.AddRole3);
this.Commands.Add(MainWindow.AddRole4);
//this.Add(new ToolStripSeparator());
this.Commands.Add(MainWindow.AddRole5);
this.Commands.Add(MainWindow.AddRole6);
this.Commands.Add(MainWindow.AddRole7);
或者,而不是让你的文本菜单绑定到命令的集合,将其绑定到FrameworkElements的集合,那么你可以直接添加任何的MenuItems或分离到集合并让菜单控制完成所有的模板....
<风格X:键=DataGridCellStyle的TargetType ={X:类型DataGridCell}>
< setter属性=文本菜单>
< Setter.Value>
<文本菜单的ItemsSource ={绑定命令}/>
< /Setter.Value>
< /二传手>
< /样式和GT;
C#:
this.Commands =新的ObservableCollection<&FrameworkElement的GT;();this.Commands.Add(新菜单项{头=菜单项2,命令= MainWindow.AddRole1});
this.Commands.Add(新菜单项{头=菜单项2,命令= MainWindow.AddRole2});
this.Commands.Add(新菜单项{头=菜单项3,命令= MainWindow.AddRole3});
this.Commands.Add(新菜单项{头=菜单项4,命令= MainWindow.AddRole4});this.Commands.Add(新分隔符);this.Commands.Add(新菜单项{头=菜单项5,命令= MainWindow.AddRole5});
this.Commands.Add(新菜单项{头=菜单项6,命令= MainWindow.AddRole6});
this.Commands.Add(新菜单项{头=菜单项7,命令= MainWindow.AddRole7});
就采用这种方法在我的应用程序 - 分隔看起来更好,这样也
。I was looking for the solution on the internet but was not able to find it within my sample. I need to add a separator between Context menu item that are generated from code behind. I tried to add it with such code lines like below but without success.
this.Commands.Add(new ToolStripSeparator());
I am wondering if someone can help. Thank you in advance.
Context Menu XAML:
<Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu ItemsSource="{Binding Commands}">
<ContextMenu.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}">
<Setter Property="Command" Value="{Binding}" />
<Setter Property="Header" Value="{Binding Path=Text}" />
<Setter Property="CommandParameter" Value="{Binding Path=Parameter}" />
</Style>
</ContextMenu.ItemContainerStyle>
</ContextMenu>
</Setter.Value>
</Setter>
C# that added in the method:
this.Commands = new ObservableCollection<ICommand>();
this.Commands.Add(MainWindow.AddRole1);
this.Commands.Add(MainWindow.AddRole2);
this.Commands.Add(MainWindow.AddRole3);
this.Commands.Add(MainWindow.AddRole4);
//this.Add(new ToolStripSeparator());
this.Commands.Add(MainWindow.AddRole5);
this.Commands.Add(MainWindow.AddRole6);
this.Commands.Add(MainWindow.AddRole7);
Or, instead of having your ContextMenu bind to a collection of commands, bind it to a collection of FrameworkElements then you can add either MenuItems or Separators directly to the collection and let the Menu control do all the templating....
<Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu ItemsSource="{Binding Commands}" />
</Setter.Value>
</Setter>
</Style>
C#:
this.Commands = new ObservableCollection<FrameworkElement>();
this.Commands.Add(new MenuItem {Header = "Menuitem 2", Command = MainWindow.AddRole1});
this.Commands.Add(new MenuItem {Header = "Menuitem 2", Command = MainWindow.AddRole2});
this.Commands.Add(new MenuItem {Header = "Menuitem 3", Command = MainWindow.AddRole3});
this.Commands.Add(new MenuItem {Header = "Menuitem 4", Command = MainWindow.AddRole4});
this.Commands.Add(new Separator);
this.Commands.Add(new MenuItem {Header = "Menuitem 5", Command = MainWindow.AddRole5});
this.Commands.Add(new MenuItem {Header = "Menuitem 6", Command = MainWindow.AddRole6});
this.Commands.Add(new MenuItem {Header = "Menuitem 7", Command = MainWindow.AddRole7});
Just used this approach in my app - the separator looks better this way also.
这篇关于如何添加水平分隔在动态创建的文本菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!