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

The Archive Base Latest Questions

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

WPF, C#, I have a datagrid with several columns, many rows. I want each

  • 0

WPF, C#,
I have a datagrid with several columns, many rows. I want each cell on a row to have different context menu item.

How to do this? thanks
I have this

<UserControl.Resources>
        <ContextMenu x:Key="cellContextMenu">
            <MenuItem x:Name="menuFillUp" Header="Fill _Up" />
        </ContextMenu>
        <Style x:Key="DataGridCellStyle" TargetType="{x:Type dg:DataGridCell}">
            <Setter Property="ContextMenu" Value="{DynamicResource cellContextMenu}" />
        </Style>
        <Style x:Key="DataGridRowStyle"  TargetType="{x:Type dg:DataGridRow}">
            <Style.Triggers>
                <Trigger Property="AlternationIndex" Value="1" >
                    <Setter Property="Background" Value="Beige" />
                </Trigger>
            </Style.Triggers>
            <Setter Property="Margin" Value="0 2 0 2" />            
        </Style>
        <Style x:Key="DataGridStyle" TargetType="{x:Type dg:DataGrid}">
            <Setter Property="AlternationCount" Value="2" />
            <Setter Property="RowStyle" Value="{StaticResource DataGridRowStyle}" />
            <Setter Property="CellStyle" Value="{StaticResource DataGridCellStyle}" />
        </Style>
</UserControl.Resources>

but this is for datagrid level. thanks

  • 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-28T23:29:20+00:00Added an answer on May 28, 2026 at 11:29 pm

    I managed this to work, xmal is not changed. In code behind of contextMenuOpening,
    I check which column is clicked, based on that, I will change header of menuitem

         private void basketDG_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            DependencyObject depObj = (DependencyObject)e.OriginalSource;
            while ((depObj != null) && !(depObj is Microsoft.Windows.Controls.DataGridCell))
            {
                depObj = VisualTreeHelper.GetParent(depObj);
            }
            if (depObj == null)
            {
                return;
            }
            if (depObj is Microsoft.Windows.Controls.DataGridCell)
            {
                var obj = depObj as Microsoft.Windows.Controls.DataGridCell;
    
                var menu = TryFindResource("cellContextMenu") as ContextMenu;
                if (menu != null && menu.Items.Count > 0)
                {
                    var menuitem = menu.Items[0] as MenuItem;
                    if (menuitem != null)
                    {
                        var col = obj.Column.Header;
                        if(col.Equals("Column1") || col.Equals("Column1") 
                            || col.Equals("Column3") || col.Equals("Column4"))
                        {
                            menuitem.Header = "Set all to " + obj;
                            menu.Visibility = Visibility.Visible;
                        }
                        else
                        {
                            menu.Visibility = Visibility.Hidden;
                        }
                    }
                }
            }
        } 
    

    However, there is one new question, all column are DataGridComboBoxColumn, the context menu shows “Set all to Microsoft.Windows.Controls.DataGridCell”, each DataGridComboBoxColumnis bound to datasource, so I do not know how to get the selectedValue of the DataGridComboBoxColumn. So my question is how to get the selected value of the DataGridComboBoxColumn?
    The other way, if I can know which row is clicked, then I will be able to figure out selectedValue from that row. but I do not know how to get which row is clicked for contextmenu, either. thanks

    Edit:
    I managed to get it this way
    var comboColumn = (obj.Content as ComboBox);
    if(comboColumn != null)
    {
    menuitem.Header = “Set all to ” + comboColumn.Text;
    menu.Visibility = Visibility.Visible;
    }

    Not elegant, but works. Anyone has a better solution, please let me know. thanks

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

Sidebar

Related Questions

I have a DataGrid in WPF app with several columns, including a Name column.
I have a WPF DataGrid bound to ObservableCollection . Each item in my collection
I have a wpf toolkit datagrid with a few columns, then inside the row
I have wpf datagrid with number of template columns. some of them have textbox
I have a WPF Datagrid control. I want to apply style for scrollbars. It
I have a WPF DataGrid (.NET 4) with custom template columns and header styles
I have a WPF Datagrid and its integer datatype columns are editable and hence
I have wpf datagrid, in one of columns, I checkboxes added in it, now
I have a wpf datagrid that has it's columns generated dynamically in code and
There is a WPF DataGrid with columns that have checkboxes. These checkboxes are already

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.