Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5994001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:45:20+00:00 2026-05-22T23:45:20+00:00

I was looking for the solution on the internet but was not able to

  • 0

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); 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:45:21+00:00Added an answer on May 22, 2026 at 11:45 pm

    EDIT:

    My first answer to this question, though it actually worked, does not follow the MVVM design principle. I am now providing an MVVM approach and leaving the original answer below for reference.

    You can create a behavior to solve this problem.

    XAML:

    <Menu>
        <MenuItem Header="_File" menu:MenuBehavior.MenuItems="{Binding Path=MenuItemViewModels, Mode=OneWay}">
    
        </MenuItem>
    </Menu>
    

    ViewModel:

    public IEnumerable<MenuItemViewModelBase> MenuItemViewModels => new List<MenuItemViewModelBase>
    {
        new MenuItemViewModel { Header = "Hello" },
        new MenuItemSeparatorViewModel(),
        new MenuItemViewModel { Header = "World" }
    };
    

    Behavior:

    public class MenuBehavior
    {
        public static readonly DependencyProperty MenuItemsProperty =
            DependencyProperty.RegisterAttached("MenuItems",
                typeof(IEnumerable<MenuItemViewModelBase>), typeof(MenuBehavior),
                new FrameworkPropertyMetadata(MenuItemsChanged));
    
        public static IEnumerable<MenuItemViewModelBase> GetMenuItems(DependencyObject element)
        {
            if (element == null)
            {
                throw (new ArgumentNullException("element"));
            }
            return (IEnumerable<MenuItemViewModelBase>)element.GetValue(MenuItemsProperty);
        }
    
        public static void SetMenuItems(DependencyObject element, IEnumerable<MenuItemViewModelBase> value)
        {
            if (element == null)
            {
                throw (new ArgumentNullException("element"));
            }
            element.SetValue(MenuItemsProperty, value);
        }
    
        private static void MenuItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var menu = (MenuItem)d;
    
            if (e.OldValue != e.NewValue)
            {
                menu.ItemsSource = ConvertViewModelsToFrameworkElements((IEnumerable<MenuItemViewModelBase>)e.NewValue);
            }
        }
    
        private static IEnumerable<FrameworkElement> ConvertViewModelsToFrameworkElements(IEnumerable<MenuItemViewModelBase> viewModels)
        {
            var frameworkElementList = new List<FrameworkElement>();
    
            foreach (var viewModel in viewModels)
            {
                switch (viewModel)
                {
                    case MenuItemViewModel mi:
                        frameworkElementList.Add(new MenuItem
                        {
                            Header = mi.Header,
                            Command = mi.Command,
                            Icon = mi.Icon
                        });
                        break;
    
                    case MenuItemSeparatorViewModel s:
                        frameworkElementList.Add(new Separator());
                        break;
                }
            }
            return frameworkElementList;
        }
    }
    

    Classes:

    public class MenuItemViewModelBase
    {
    }
    
    public class MenuItemViewModel : MenuItemViewModelBase
    {
        public object Header { get; set; }
        public ICommand Command { get; set; }
        public object Icon { get; set; }
    }
    
    public class MenuItemSeparatorViewModel : MenuItemViewModelBase
    {
    }
    

    Original Answer:

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been looking all over the internet and I can't find an acceptable solution
I've been looking all over the internet for a solution to the following error;
I'm looking for a solution to find out about the MAC number of a
I'm looking for a solution like the one discussed here, but for C# WinForms.
I have scoured the internet looking for a solution to this and I am
I'm trying to find a solution similar to http://www.tenstreet.com . I've tried looking around
I have looked around the internet for 3 hours now looking for a solution
I'm looking for the easiest solution to fixing a problem I have in Internet
Ok, I searched the internet and stackoverflow but I just can't seem to find
I'm looking for solution to extract some node from large xml file (using xmlstarlet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.