Datagrid Selected Cells Row Index and Column Index into list. Using Attached Property. Huge Performance Issue.
public class RowNumberDp { public static DependencyProperty DisplayRowNumberProperty = DependencyProperty.RegisterAttached("DisplayRowNumber", typeof(bool), typeof(RowNumberDp), new FrameworkPropertyMetadata(false, OnDisplayRowNumberChanged)); public static bool GetDisplayRowNumber(DependencyObject target) { return (bool) target.GetValue(DisplayRowNumberProperty); } public static void SetDisplayRowNumber(DependencyObject target, bool value) { target.SetValue(DisplayRowNumberProperty, value); } private static void OnDisplayRowNumberChanged(DependencyObject target, DependencyPropertyChangedEventArgs e) { if (!(target is DataGrid dataGrid)) return; dataGrid.SelectedCellsChanged += Selectedcells; } private static void Selectedcells(object sender, SelectedCellsChangedEventArgs e) { if (!(sender is DataGrid datagrid)) return; foreach (DataGridCellInfo obj in datagrid.SelectedCells) { var rowIndex = datagrid.Items.IndexOf(obj.Item) var ColumnName = obj.Column.SortMemberPath; } } }
Problem Statement : Row Index is
causing huge performance Issue ...If I comment rowIndex everything works Super Fast..
I need rowindex for SelectedCells.
I tried Different approach as well.
1.
private static List<Tuple<int, int>> GetRowAndColumnIndicesFromGivenCell(DataGrid dataGrid, DataGridCell cell) { List<Tuple<int, int>> cellList = null; var columnIndex = cell.Column.DisplayIndex; var parent = VisualTreeHelper.GetParent(cell); while (parent != null && parent.GetType() != typeof(DataGridRow)) { parent = VisualTreeHelper.GetParent(parent); } if (parent is DataGridRow) { var dataGridRow = parent as DataGridRow; var item = dataGridRow.Item; if (item != null) { var rowIndex = dataGrid.Items.IndexOf(item); if (rowIndex != -1) { cellList = new List<Tuple<int, int>>() { new Tuple<int, int>(rowIndex, columnIndex) }; } } } return cellList; }
from above code...Only Visible Datagrid Rows will come....But Scrolling does not happen,
Example If i select 1 to 1000 Only 986 to 1000 Numbers will Show.