New to Telerik UI for WPF? Download free 30-day trial

Select a Cell Region by Dragging the Mouse from One Cell to Another


Product Version 2022.3.912
Product RadVirtualGrid for WPF


How to select a RadVirtualGrid cell region by dragging the mouse from one cell to another.


To achieve this requirement, first set up handlers for the MouseLeftButtonDown and MouseLeftButtonUp events of RadVirtualGrid:

Adding the event handlers

this.VirtualGrid.AddHandler(RadVirtualGrid.MouseLeftButtonDownEvent, new MouseButtonEventHandler(VirtualGrid_MouseLeftButtonDown), true); 
this.VirtualGrid.AddHandler(RadVirtualGrid.MouseLeftButtonUpEvent, new MouseButtonEventHandler(VirtualGrid_MouseLeftButtonUp), true); 
Note that we do this with the AddHandler method so that we can set the handledEventsToo parameter. This is required, because the RadVirtualGrid internally handles these events and our custom handlers will not be invoked otherwise.

Then, in the MouseLeftButtonDown event handler, with the help of the GetRowIndexAtMousePosition and GetColumnIndexAtMousePosition methods we get ahold of the row and column indexes of the initial cell which will serve as an anchor for the selected region.

Handling the MouseLeftButtonDown event

private int startRowIndex = -1; 
private int startColumnIndex = -1; 
private void VirtualGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    var virtualGrid = sender as RadVirtualGrid; 
    var border = e.OriginalSource as CanvasInputBorder; 
    this.startRowIndex = virtualGrid.GetRowIndexAtMousePosition(border); 
    this.startColumnIndex = virtualGrid.GetColumnIndexAtMousePosition(border); 
Then in a similar fashion, we can obtain the last selected cell in the MouseLeftButtonUp event handler. Then, based on the start and end cells, we can create a new CellRegion and use the SelectCellRegion method to select it.

Handling the MouseLeftButtonDown event

private void VirtualGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
    var virtualGrid = sender as RadVirtualGrid; 
    var border = e.OriginalSource as CanvasInputBorder; 
    var endColumnIndex = virtualGrid.GetColumnIndexAtMousePosition(border); 
    var endRowIndex = virtualGrid.GetRowIndexAtMousePosition(border); 
    var left = Math.Min(startColumnIndex, endColumnIndex); 
    var top = Math.Min(startRowIndex, endRowIndex); 
    var width = Math.Abs(startColumnIndex - endColumnIndex) + 1; 
    var height = Math.Abs(startRowIndex - endRowIndex) + 1; 
    virtualGrid.SelectCellRegion(new CellRegion(left, top, width, height)); 

You can also perform the logic of the MouseLeftButtonUp event on MouseMove but you will need to add some additional logic to stop the selection process when the mouse button is released.

See Also

In this article