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

Get the Row or Column Index of the Mouse Position

In a scenario when the row/column index for the mouse position is needed, the GetRowIndexAtMousePosition and GetColumnIndexAtMousePosition methods come in handy. In order to return the needed index, they accept a parameter of type CanvasInputBorder. This Border is used internally by RadVirtualGrid to enable mouse interaction with the control. For example, if the row and column indexes for the MouseRightButtonDown event of RadVirtualGrid are needed, its event handler would be similar to the following one.

Getting the Row and Column Index on MouseRightButtonDown event

private void VirtualGrid_MouseRightButtonDown(object sender, MouseButtonEventArgs e) 
    { 
        var border = e.OriginalSource as Telerik.Windows.Controls.VirtualGrid.CanvasInputBorder; 
        var columnIndex = this.VirtualGrid.GetColumnIndexAtMousePosition(border); 
        var rowIndex = this.VirtualGrid.GetRowIndexAtMousePosition(border); 
    } 

If you have defined a custom CellTemplate, you would need to add additional logic to get ahold of the CanvasInputBorder.

Getting the Row and Column Index when using a custom CellTemplate

private void VirtualGrid_MouseRightButtonDown(object sender, MouseButtonEventArgs e) 
    {    
    var border = e.OriginalSource as Telerik.Windows.Controls.VirtualGrid.CanvasInputBorder; 
    if (border == null) 
    { 
        var element = e.OriginalSource as FrameworkElement; 
        var panel = element.ParentOfType<VirtualizingCanvasBase>(); 
        border = panel.ChildrenOfType<CanvasInputBorder>().FirstOrDefault(); 
    } 
 
    var columnIndex = this.VirtualGrid.GetColumnIndexAtMousePosition(border); 
    var rowIndex = this.VirtualGrid.GetRowIndexAtMousePosition(border); 
    } 

The previous examples show how to use the MouseRightButtonDown event. To subscribe to MouseLeftButtonDown, you will need to use the AddHandler method with its last parameter set to true. this.virtualGrid.AddHandler(RadVirtualGrid.MouseLeftButtonDownEvent, new MouseButtonEventHandler(virtualGrid_MouseLeftButtonDown), true);

See Also

In this article