This sample show cases how to improve the performance while selecting the row using shift key in WPF TreeGrid (SfTreeGrid).
In TreeGrid, the performance of selecting the rows using Shift key can be improved by overriding the TreeGridRowSelectionController.SelectRows method.
The sample explains the performance improvement using Shift key.
public MainWindow()
{
InitializeComponent();
treeGrid.SelectionController = new TreeGridSelectionControllerExt(treeGrid);
} The ProcessPointerReleased override method is used to call the SelectRows method when pressing Shift key and releasing the pointer.
public class TreeGridSelectionControllerExt : TreeGridRowSelectionController
{
SfTreeGrid treeGrid;
public TreeGridSelectionControllerExt(SfTreeGrid TreeGrid) : base(TreeGrid)
{
treeGrid = TreeGrid;
}
/// <summary>
/// Processes the row selection when the mouse pointer is released from SfTreeGrid.
/// </summary>
/// <param name="args">
/// Contains the data for mouse pointer action.
/// </param>
/// <param name="rowColumnIndex">
/// The corresponding rowColumnIndex of the mouse released point.
/// </param>
/// <remarks>
/// The selection is initialized in pointer released state when the <see cref="Syncfusion.UI.Xaml.TreeGrid.SfTreeGrid.AllowSelectionPointerPressed"/> set as false.
/// </remarks>
protected override void ProcessPointerReleased(MouseButtonEventArgs args, Syncfusion.UI.Xaml.ScrollAxis.RowColumnIndex rowColumnIndex)
{
if (SelectionHelper.CheckShiftKeyPressed() && this.PressedRowColumnIndex.RowIndex > 0)
{
this.SelectRows(this.PressedRowColumnIndex.RowIndex, rowColumnIndex.RowIndex);
}
else
base.ProcessPointerReleased(args, rowColumnIndex);
}
/// <summary>
/// Selects the rows corresponding to the specified start and end index of the row.
/// </summary>
/// <param name="startRowIndex">
/// The start index of the row.
/// </param>
/// <param name="endRowIndex">
/// The end index of the row.
/// </param>
public override void SelectRows(int startRowIndex, int endRowIndex)
{
if (startRowIndex < 0 || endRowIndex < 0)
return;
if (startRowIndex > endRowIndex)
{
var temp = startRowIndex;
startRowIndex = endRowIndex;
endRowIndex = temp;
}
if (this.treeGrid.SelectionMode == GridSelectionMode.None ||
this.treeGrid.SelectionMode == GridSelectionMode.Single)
return;
var isSelectedRowsContains = this.SelectedRows.Any();
this.SuspendUpdates();
var addedItem = new List<object>();
int rowIndex = startRowIndex;
this.treeGrid.SelectedItems.Clear();
var rowIndexes = this.treeGrid.SelectionController.SelectedRows.Select(rowinfo => rowinfo.RowIndex).ToList();
var selectedrowindex = this.treeGrid.ResolveToRowIndex(this.treeGrid.SelectedIndex);
string className = "Syncfusion.UI.Xaml.TreeGrid.Helpers.TreeGridSelectionHelper, Syncfusion.SfGrid.WPF";
Type helperType = Type.GetType(className);
Type[] parameterTypes = new Type[] { typeof(int) };
Type[] HelperParameterTypes = new Type[]
{
typeof(SfTreeGrid),
typeof(int),
typeof(TreeNode)
};
// Getting the required methods using Reflection.
MethodInfo GetTreeGridSelectedRow = (this as TreeGridBaseSelectionController).GetType().GetMethod("GetTreeGridSelectedRow",BindingFlags.Instance | BindingFlags.NonPublic, null, parameterTypes, null);
MethodInfo ShowRowSelection = helperType.GetMethod("ShowRowSelection", BindingFlags.Static | BindingFlags.NonPublic, null, HelperParameterTypes, null
);
for (int i = rowIndex; i <= endRowIndex; i++)
{
object rowData = this.treeGrid.GetNodeAtRowIndex(i);
if (!rowIndexes.Contains(i))
{
this.SelectedRows.Add((TreeGridRowInfo)GetTreeGridSelectedRow.Invoke(this, new object[] { i }));
ShowRowSelection.Invoke(null, new object[] { treeGrid, i, rowData });
}
if (rowData != null)
{
this.treeGrid.SelectedItems.Add(rowData);
}
}
if (!isSelectedRowsContains)
{
this.treeGrid.SelectedIndex = this.SelectedRows.Count > 0 ? this.treeGrid.ResolveToNodeIndex(this.SelectedRows[0].RowIndex) : -1;
this.treeGrid.SelectedItem = this.treeGrid.SelectedItems.Count > 0 ? this.treeGrid.SelectedItems[0] : null;
}
this.ResumeUpdates();
}
}