如何在动态创建的ContextMenu中添加水平分隔符?

vla*_*c77 20 wpf binding wpf-controls wpfdatagrid

我在互联网上寻找解决方案,但无法在我的样本中找到它.我需要在从代码隐藏生成的Context菜单项之间添加一个分隔符.我尝试使用如下代码行添加它但没有成功.

this.Commands.Add(new ToolStripSeparator()); 
Run Code Online (Sandbox Code Playgroud)

我想知道是否有人可以提供帮助.先感谢您.

上下文菜单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>
Run Code Online (Sandbox Code Playgroud)

在方法中添加的C#:

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); 
Run Code Online (Sandbox Code Playgroud)

Rac*_*hel 47

我这样做了一次并使用了一个null作为我的分隔符.从XAML开始,如果datacontext为null,我将模板设置为使用分隔符

代码背后:

this.Commands.Add(MainWindow.AddRole4);
this.Add(null); 
this.Commands.Add(MainWindow.AddRole5);
Run Code Online (Sandbox Code Playgroud)

XAML是这样的:

<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.Triggers>
            <DataTrigger Binding="{Binding }" Value="{x:Null}">
                <Setter Property="Template" Value="{StaticResource MenuSeparatorTemplate}" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</ContextMenu.ItemContainerStyle>
Run Code Online (Sandbox Code Playgroud)

希望我的语法正确 - 我在这台机器上没有IDE来验证代码

编辑

这是上下文菜单分隔符的示例模板.我把它ContextMenu.Resources放进去,虽然你可以把它放在你想要的任何地方,只要ContextMenu可以访问它.

<ContextMenu.Resources>
    <ControlTemplate x:Key="MenuSeparatorTemplate">
        <Separator />
    </ControlTemplate>
</ContextMenu.Resources>
Run Code Online (Sandbox Code Playgroud)


sam*_*ric 12

或者,不是将ContextMenu绑定到命令集合,而是将其绑定到FrameworkElements的集合,然后您可以将MenuItems或Separator直接添加到集合中,让Menu控件执行所有模板....

<Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}">
    <Setter Property="ContextMenu">
        <Setter.Value>
            <ContextMenu ItemsSource="{Binding Commands}" />
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

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});
Run Code Online (Sandbox Code Playgroud)

刚刚在我的应用中使用了这种方法 - 分隔符看起来也更好.

  • 如果你想保持模型 - 视图 - 分离,这不是一个好主意. (16认同)
  • Downvoted是因为您不应将MenuItem放在ViewModel中.它是一个ViewModel,而不是一个视图. (5认同)

Tin*_*Man 11

我已经修改了上面 Rachel 提供的解决方案以更正 Separator 样式。我意识到这篇文章很旧,但仍然是谷歌上最好的结果之一。在我的情况下,我将它用于 Menu 和 ContextMenu,但同样应该可以工作。

XAML

<Menu ItemsSource="{Binding MenuItems}">
    <Menu.Resources>
        <ControlTemplate x:Key="MenuSeparatorTemplate">
            <Separator>
                <Separator.Style>
                    <Style TargetType="{x:Type Separator}" BasedOn="{StaticResource ResourceKey={x:Static MenuItem.SeparatorStyleKey}}"/>
                </Separator.Style>
            </Separator>
        </ControlTemplate>
        <Style TargetType="{x:Type MenuItem}">
            <Setter Property="Header" Value="{Binding MenuItemHeader}" />
            <Setter Property="Command" Value="{Binding MenuItemCommand}" />
            <Setter Property="CommandParameter" Value="{Binding MenuItemCommandParameter}" />
            <Setter Property="ItemsSource" Value="{Binding MenuItemCollection}" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding }" Value="{x:Null}">
                    <Setter Property="Template" Value="{StaticResource MenuSeparatorTemplate}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Menu.Resources>
</Menu>
Run Code Online (Sandbox Code Playgroud)

无分隔符样式更改

带分隔符样式更改