I want to select multiple rows by selecting one and then holding down the shift key and selecting more.
For example in WPF DataGrid there is an Extended multiple selection option: https://docs.telerik.com/devtools/wpf/controls/radgridview/selection/multiple-selection
Solution:
1. use a custom CellTap command
2. In the Cell Tap Command Execute method define Routes to single or range selection based on Shift key state
public override void Execute(object parameter)
{
if (!IsShiftKeyDown())
{
HandleSingleSelection(parameter);
return;
}
HandleRangeSelection(parameter);
}The Single Selection clears the selection and executes default tap behavior
The Range Selection:
- Gets anchor item
- Finds start/end indexes in data
- Clears selections, re-adds anchor
- Selects all items between anchor and clicked item
Here is the HandleRangeSelection implementation:
private void HandleRangeSelection(object parameter)
{
var selectedItem = this.Owner.SelectedItem;
if (selectedItem == null)
{
return;
}
var info = (DataGridCellInfo)parameter;
var dataView = this.Owner.GetDataView();
var items = dataView.Items.ToList();
var lastSelectedIndex = items.IndexOf(selectedItem);
var nextSelectedIndex = items.IndexOf(info.Item);
if (lastSelectedIndex == -1 || nextSelectedIndex == -1)
{
return;
}
this.Owner.SelectedItems.Clear();
this.Owner.SelectedItems.Add(selectedItem);
SelectItemsInRange(items, lastSelectedIndex, nextSelectedIndex);
}To access the IsShiftKeyDown internal method, you have to use a reflection:
private bool IsShiftKeyDown()
{
var method = typeof(RadDataGrid).GetMethod("IsShiftKeyDown", BindingFlags.Instance | BindingFlags.NonPublic);
return method?.Invoke(this.Owner, null) as bool? ?? false;
}Regards,
Didi
Progress Telerik