Articles in this section
Category / Section

How to prevent the selection while pressing right-click in WPF DataGrid (SfDataGrid)?

1 min read

You can prevent selection when right clicking in WPF DataGrid (SfDataGrid) by customizing the SelectionController and overriding the ProcessPointerPressed method.

Refer the below code example in which the row selection for SfDataGrid is skipped when the IsRightButtonPressed property is true.

WPF

protected override void ProcessPointerPressed(MouseButtonEventArgs args, RowColumnIndex rowColumnIndex)
{
    if (args.ChangedButton == MouseButton.Right)
    {
        args.Handled = true;
    }
    else
        base.ProcessPointerPressed(args, rowColumnIndex);
}

UWP

this.datagrid.SelectionController = new GridSelectionControllerExt(datagrid);
public class GridSelectionControllerExt : GridSelectionController
{
    public GridSelectionControllerExt(SfDataGrid dataGrid)
        : base(dataGrid)
    { }
    protected override void ProcessPointerPressed(PointerRoutedEventArgs args, RowColumnIndex rowColumnIndex)
    {
        var properties = args.GetCurrentPoint(DataGrid).Properties;
        if (properties.IsRightButtonPressed)
        {
            args.Handled = true;
        }
        else
            base.ProcessPointerPressed(args, rowColumnIndex);
    }
}

 

Selection is disabled while right click on row of WPF DataGrid

Conclusion

I hope you enjoyed learning about how to prevent the selection while pressing right-click in WPF DataGrid (SfDataGrid).

You can refer to our WPF Grid feature tour page to know about its other groundbreaking feature representations. You can also explore our WPF Grid documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied