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 4025556
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:51:41+00:00 2026-05-20T10:51:41+00:00

I have a datagrid that potentially can have many rows. As the user right

  • 0

I have a datagrid that potentially can have many rows. As the user right clicks one of the rows, I need to show a context menu for each of the rows and perform an action (same action but different data item according to the current selected row) when the user clicks the option.

What is the best strategy for this?

I’m fearing that a ContextMenu for each row is overkill even though I’m creating the menu using the ContextMenuOpening event, sort of a “lazy load” for the context menu. Should I only use one ContextMenu for the datagrid? But with this I would have some more work regarding the click event, to determine the correct row, etc.

  • 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-20T10:51:42+00:00Added an answer on May 20, 2026 at 10:51 am

    As far as I know, some of the actions will be disabled or enabled depending on the row, so there is no point in a single ContextMenu for a DataGrid.

    I have an example of the row-level context menu.

    <UserControl.Resources>
        <ContextMenu  x:Key="RowMenu" DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
            <MenuItem Header="Edit" Command="{Binding EditCommand}"/>
        </ContextMenu>
        <Style x:Key="DefaultRowStyle" TargetType="{x:Type DataGridRow}">
            <Setter Property="ContextMenu" Value="{StaticResource RowMenu}" />
        </Style>
    </UserControl.Resources>
    
    <DataGrid RowStyle="{StaticResource DefaultRowStyle}"/>
    

    The DataGrid must have a binding to a list of view models with commands:

    public class ItemModel
    {
        public ItemModel()
        {
            this.EditCommand = new SimpleCommand 
            { 
                ExecuteDelegate = _ => MessageBox.Show("Execute"), 
                CanExecuteDelegate = _ => this.Id == 1 
            };
        }
        public int Id { get; set; }
        public string Title { get; set; }
        public ICommand EditCommand { get; set; }
    }
    

    The context menu is created in the resources collection of the UserControl and I think there is only one object which is connected with datagrid rows by reference, not by value.

    Here is another example of ContextMenu for a Command inside a MainViewModel. I suppose that DataGrid has a correct view model as the DataContext, also the CommandParameter attribute must be placed before the Command attribute:

        <ContextMenu  x:Key="RowMenu" DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
            <MenuItem Header="Edit" CommandParameter="{Binding}"
                      Command="{Binding DataContext.DataGridActionCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}" />
        </ContextMenu>
    

    Models:

    public class MainViewModel
    {
        public MainViewModel()
        {
            this.DataGridActionCommand = new DelegateCommand<ItemModel>(m => MessageBox.Show(m.Title), m => m != null && m.Id != 2);
        }
    
        public DelegateCommand<ItemModel> DataGridActionCommand { get; set; }
        public List<ItemModel> Items { get; set; }
    }
    
    public class ItemModel
    {
        public int Id { get; set; }
        public string Title { get; set; }
    }
    

    But there is a problem that MenuItem isn’t displayed as a disabled item if CanExecute returns false. The possible workaround is using a ParentModel property inside the ItemModel, but it doesn’t differ much from the first solution.
    Here is example of above-described solution:

    public class ItemModel
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public MainViewModel ParentViewModel { get; set; }
    }
    
    //Somewhere in the code-behind, create the main view model 
    //and force child items to use this model as a parent model
    var mainModel = new MainViewModel { Items = items.Select(item => new ItemViewModel(item, mainModel)).ToList()};
    

    And MenuItem in XAML will be simplier:

    <MenuItem Header="Edit" CommandParameter="{Binding}"
                  Command="{Binding ParentViewModel.DataGridActionCommand}" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a datagrid that I want the user to sort the rows on.
I have a DataGrid that has items in it. When you right-click on one
I have a DataGrid that needs to show a gradient background on mouseover of
I have a DataGrid that is showing some data via a PagedCollectionView with one
I have a datagrid that is bound to a observableCollection of Employees The user
Consider i have a dataGrid that is created dynamically. Now i need to add
I have a datagrid that allows user to change and save columns widths and
I have DataGrid that connects to REST service. I need to pass some custom
I have a DataGrid that I am loading with a significant number of rows
I have a DataGrid that two of its columns are ComboBoxes (one contains few

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.