Click here to Skip to main content
15,885,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In my program I have a datagrid which is bound to an observable list. I want to be able to set the context menu to only display when a row is selected so that there wont be errors with nothing being selected. To do this I would like to be able to disable the context menu on the column headers. Here is the XAML:

XAML
<DataGrid Name="dgItems" AutoGenerateColumns="False" SelectionMode="Single" SelectionUnit="FullRow" CanUserAddRows="False" IsReadOnly="True">
  <DataGrid.ContextMenu>
      <ContextMenu>
          <MenuItem Header="Edit Item" Click="EditItem_Click"/>
          <MenuItem Header="Delete Item" Click="DeleteItem_Click"/>
      </ContextMenu>
  </DataGrid.ContextMenu>
   <DataGrid.Columns>
      <DataGridTextColumn Header="Caption" Binding="{Binding Caption}"/>
      <DataGridTextColumn Header="Details" Binding="{Binding Details}"/>
      <DataGridTextColumn Header="DateFrom" Binding="{Binding DateFrom}"/>
      <DataGridTextColumn Header="DateTo" Binding="{Binding DateTo}"/>
      <DataGridTextColumn Header="Source" Binding="{Binding Source}"/>
   </DataGrid.Columns>
</DataGrid>

How would I go about disabling the context menus on the headers? Thanks.
Posted
Updated 23-Nov-18 16:25pm

Use this code in PreviewMouseRightButtonDown event of DataGrid,
C#
private void DataGrid_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
    DependencyObject DepObject = (DependencyObject)e.OriginalSource;

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

    if (DepObject == null)
    {
        return;
    }

    if (DepObject is DataGridColumnHeader)
    {
          dgItems.ContextMenu.Visibility = Visibility.Collapsed;
    }
    else
    {
          dgItems.ContextMenu.Visibility = Visibility.Visible;
    }
}
 
Share this answer
 
v2
The proposed solution above does not work (or did not work for me anyway) as you need to test for DataGridRow as well, otherwise you always get null if you are actually right-clicking on a valid row. Here is what worked correctly for me:

        private void dgMP_MOCParts_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            DependencyObject DepObject = (DependencyObject)e.OriginalSource;

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

            if (DepObject == null)
            {
                return;
            }

            if (DepObject is DataGridColumnHeader)
            {
                dgMP_MOCParts.ContextMenu.Visibility = Visibility.Collapsed;
            }
            else
            {
                dgMP_MOCParts.ContextMenu.Visibility = Visibility.Visible;
            }
        }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900