1

I currently have code that shows a ContextMenu upon right clicking. My problem however stems from the fact that the menu pops up when right clicking anywhere on the DataGrid and not just the rows. Is there anyway to prevent this? i.e., have the ContextMenu pop up only when right clicking a row.

// C#

    private void updateBill(object sender, RoutedEventArgs e) {
        Console.WriteLine("WORKS");
    }

// XAML

                <DataGrid x:Name="dgCash" HorizontalAlignment="Left" Margin="5,0,5,5" Grid.RowSpan="1" AutoGenerateColumns="False" IsReadOnly="True"
              HorizontalContentAlignment="Right">
                    <DataGrid.Columns>
                        <DataGridTextColumn Binding="{Binding denomination}" Header="Denomination"/>
                        <DataGridTextColumn Binding="{Binding start}" Header="Starting"/>
                        <DataGridTextColumn Binding="{Binding end}" Header="End of day"/>
                        <DataGridTextColumn Binding="{Binding deposit}" Header="Deposit" Width="*"/>
                    </DataGrid.Columns>
                    <DataGrid.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="Update Bill" Click="updateBill" />
                        </ContextMenu>
                    </DataGrid.ContextMenu>
                </DataGrid>
Lorenzo Ang
  • 1,202
  • 3
  • 12
  • 35
  • 1
    Possible duplicate of [Create contextmenus for datagrid rows](http://stackoverflow.com/questions/5200687/create-contextmenus-for-datagrid-rows) – ASh Nov 16 '16 at 07:36
  • 1
    Hi! I tried that and it did work but it also introduced a bug that ended up hiding the entire row when selected. I'm not sure why but maybe it has something to do with the theme that I'm using? I'm currently running on `MetroWindow` – Lorenzo Ang Nov 17 '16 at 06:33
  • @Dilisqq you are correct.. for me, after right clicking the datagridrow, its contents disappear, but the row is there, just a blank row... Have you figure it out why? – JobaDiniz Jun 26 '17 at 14:51

0 Answers0