如何仅在WPF DataGridRow上显示ContextMenu

时间:2016-11-16 07:07:57

标签: c# wpf datagrid

我目前的代码在右键单击时显示ContextMenu。然而,我的问题源于这样一个事实:右键单击DataGrid上的任何位置时弹出菜单而不仅仅是行。反正有没有阻止这个?即,只有在右键单击一行时才弹出ContextMenu

// 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>

0 个答案:

没有答案