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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:12:21+00:00 2026-05-17T22:12:21+00:00

I have defined a datagrid like this :: <cc:PEDataGrid AutoGenerateColumns=False ItemsSource={Binding Rows} Width=Auto PreviewMouseRightButtonDown=PEGrid_PreviewMouseRightButtonDown

  • 0

I have defined a datagrid like this ::

 <cc:PEDataGrid AutoGenerateColumns="False"
               ItemsSource="{Binding Rows}"
               Width="Auto"                
               PreviewMouseRightButtonDown="PEGrid_PreviewMouseRightButtonDown"
               Loaded="CommonPEGrid_Loaded">             
      <wpfkit:DataGrid.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Cut" />
            <MenuItem Header="Copy"/>
            <MenuItem Header="Paste"/>              
        </ContextMenu>
    </wpfkit:DataGrid.ContextMenu>
</cc:PEDataGrid>

This shows contextMenu on every cell when right click is done.
I want to disable context menu for all the cells except headers and also on header for some condition. (I dont want to use DataGridHeaderStyle because of some other problems which I dont want to explain here.)

I have defined a handler for PreviewMouseRightButtonDown on the datagrid and in the handler I am trying to do something like this::

    private void PEGrid_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
    {
        DependencyObject depObj = (DependencyObject)e.OriginalSource;

        while ((depObj != null) && !(depObj is DataGridColumnHeader))
        {
            depObj = VisualTreeHelper.GetParent(depObj);
        }

        if (depObj == null)
        {
            return;
        }

        if (depObj is DataGridColumnHeader)
        {
            //some condition here which says whether contextmenu is required on this header

            (depObj as DataGridColumnHeader).ContextMenu = null;
            //the above line is not working!!!!
        }
        else
        {
             (depObj as DataGridCell).ContextMenu = null;
              //the above line not working!!!!
         }
    }

I want to know where am I going wrong!! Please help me regarding this. Also guide me to do in a better way if I am achieving my requirement in a wrong way 🙂

  • 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-17T22:12:22+00:00Added an answer on May 17, 2026 at 10:12 pm

    Hey I Solved it :: My modified code is as follows:::

    private void PEGrid_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
    {
        DependencyObject depObj = (DependencyObject)e.OriginalSource;
    
        while ((depObj != null) && !(depObj is DataGridColumnHeader))
        {
            depObj = VisualTreeHelper.GetParent(depObj);
        }
    
        if (depObj == null)
        {
            return;
        }
    
        if (depObj is DataGridColumnHeader)
        {
              dg.ContextMenu.Visibility = Visibility.Visible;  //works
        }
        else
        {
              dg.ContextMenu.Visibility = Visibility.Collapsed; //works
         }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DataGrid which is as follows:: <wpfkit:DataGrid AutoGenerateColumns=False ItemsSource={Binding} Width=Auto FrozenColumnCount=2 SelectionMode=Extended
I have a class defined like this: class TouchScreenDisabledGrid:DataGrid,INotifyPropertyChanged I want to change soome
I have a datagrid with a column of comboboxes defined like this: <DataGridTemplateColumn x:Name=AssortmentQualitySettingsDataGridColumn
I have defined a rectangle drawable like this: <shape xmlns:android=http://schemas.android.com/apk/res/android android:shape=rectangle> <corners android:radius=4dip/> <solid
I have a comboxbox defined like this (basically): <ComboBox x:Name=pageViewSize> <ComboBox.Items> <ComboBoxItem IsSelected=True>5</ComboBoxItem> <ComboBoxItem>10</ComboBoxItem>
I have defined the datagrid as follows <mx:DataGrid id=dg width=100% height=100% > In the
I have defined an array like so : var myArray = {myNewArray: ['string1' ,
I have something like orders table bound to a DataGrid . I want to
I have a datagrid defined in XAML as follows: <toolkit:DataGrid Margin=10,116,62,34 Name=WADataGrid RowBackground=LightYellow AlternatingRowBackground=White
I have a ContextMenu defined on a Datagrid but want to bind submenu items

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.