@@ -75,7 +75,7 @@ private void TableView_SelectionChanged(object sender, SelectionChangedEventArgs
75
75
} ) ;
76
76
}
77
77
78
- SetCurrentCell ( null ) ;
78
+ CurrentCellSlot = null ;
79
79
OnCellSelectionChanged ( ) ;
80
80
81
81
if ( SelectedIndex > 0 )
@@ -192,7 +192,7 @@ private void HandleNavigations(KeyRoutedEventArgs e, bool shiftKey, bool ctrlKey
192
192
if ( isEditing && currentCell is not null )
193
193
{
194
194
currentCell = GetCellFromSlot ( newSlot ) ;
195
- currentCell . PrepareForEdit ( ) ;
195
+ currentCell ? . PrepareForEdit ( ) ;
196
196
}
197
197
198
198
e . Handled = true ;
@@ -803,7 +803,7 @@ public void RefreshFilter()
803
803
if ( SelectionUnit is TableViewSelectionUnit . Cell )
804
804
{
805
805
SelectAllCells ( ) ;
806
- SetCurrentCell ( null ) ;
806
+ CurrentCellSlot = null ;
807
807
}
808
808
else
809
809
{
@@ -886,7 +886,7 @@ private void DeselectAllCells()
886
886
{
887
887
SelectedCellRanges . Clear ( ) ;
888
888
OnCellSelectionChanged ( ) ;
889
- SetCurrentCell ( null ) ;
889
+ CurrentCellSlot = null ;
890
890
}
891
891
892
892
/// <summary>
@@ -922,17 +922,17 @@ internal void MakeSelection(TableViewCellSlot slot, bool shiftKey, bool ctrlKey
922
922
{
923
923
SelectRows ( slot , shiftKey ) ;
924
924
LastSelectionUnit = TableViewSelectionUnit . Row ;
925
- }
926
- else
927
- {
925
+ }
926
+ else
927
+ {
928
928
SelectCells ( slot , shiftKey ) ;
929
929
LastSelectionUnit = TableViewSelectionUnit . Cell ;
930
930
}
931
931
}
932
932
else if ( ! IsReadOnly )
933
933
{
934
934
SelectionStartCellSlot = slot ;
935
- DispatcherQueue . TryEnqueue ( ( ) => SetCurrentCell ( slot ) ) ;
935
+ CurrentCellSlot = slot ;
936
936
}
937
937
}
938
938
@@ -948,14 +948,14 @@ private void SelectRows(TableViewCellSlot slot, bool shiftKey)
948
948
if ( selectionRange is not null )
949
949
{
950
950
DeselectRange ( selectionRange ) ;
951
- }
951
+ }
952
952
953
953
if ( shiftKey && SelectionMode is ListViewSelectionMode . Multiple or ListViewSelectionMode . Extended )
954
954
{
955
955
var min = Math . Min ( SelectionStartRowIndex . Value , slot . Row ) ;
956
956
var max = Math . Max ( SelectionStartRowIndex . Value , slot . Row ) ;
957
957
958
- SelectRange ( new ItemIndexRange ( min , ( uint ) ( max - min ) + 1 ) ) ;
958
+ SelectRange ( new ItemIndexRange ( min , ( uint ) ( max - min ) + 1 ) ) ;
959
959
}
960
960
else
961
961
{
@@ -968,11 +968,11 @@ private void SelectRows(TableViewCellSlot slot, bool shiftKey)
968
968
{
969
969
SelectRange ( new ItemIndexRange ( slot . Row , 1 ) ) ;
970
970
}
971
- }
971
+ }
972
972
973
973
if ( ! IsReadOnly && slot . IsValid ( this ) )
974
974
{
975
- DispatcherQueue . TryEnqueue ( ( ) => SetCurrentCell ( slot ) ) ;
975
+ CurrentCellSlot = slot ;
976
976
}
977
977
else
978
978
{
@@ -1033,7 +1033,7 @@ private void SelectCells(TableViewCellSlot slot, bool shiftKey)
1033
1033
1034
1034
SelectedCellRanges . Add ( selectionRange ) ;
1035
1035
OnCellSelectionChanged ( ) ;
1036
- DispatcherQueue . TryEnqueue ( ( ) => SetCurrentCell ( slot ) ) ;
1036
+ CurrentCellSlot = slot ;
1037
1037
}
1038
1038
1039
1039
/// <summary>
@@ -1049,38 +1049,32 @@ internal void DeselectCell(TableViewCellSlot slot)
1049
1049
SelectedCellRanges . Remove ( selectionRange ) ;
1050
1050
}
1051
1051
1052
- SetCurrentCell ( slot ) ;
1052
+ CurrentCellSlot = slot ;
1053
1053
OnCellSelectionChanged ( ) ;
1054
1054
}
1055
1055
1056
1056
/// <summary>
1057
- /// Sets the current cell based on the specified cell slot .
1057
+ /// Handles changes to the current cell in the table view .
1058
1058
/// </summary>
1059
- internal async void SetCurrentCell ( TableViewCellSlot ? slot )
1059
+ private async Task OnCurrentCellChanged ( TableViewCellSlot ? oldSlot , TableViewCellSlot ? newSlot )
1060
1060
{
1061
- if ( slot == CurrentCellSlot )
1061
+ if ( oldSlot == newSlot )
1062
1062
{
1063
1063
return ;
1064
1064
}
1065
1065
1066
- var oldSlot = CurrentCellSlot ;
1067
- var currentCell = oldSlot . HasValue ? GetCellFromSlot ( oldSlot . Value ) : default ;
1068
- currentCell ? . SetElement ( ) ;
1069
- CurrentCellSlot = slot ;
1070
-
1071
- if ( oldSlot is { } )
1066
+ if ( oldSlot . HasValue )
1072
1067
{
1073
- var row = _rows . FirstOrDefault ( x => x . Index == oldSlot . Value . Row ) ;
1074
- row ? . ApplyCurrentCellState ( oldSlot . Value ) ;
1068
+ var cell = GetCellFromSlot ( oldSlot . Value ) ;
1069
+ cell ? . SetElement ( ) ;
1070
+ cell ? . ApplyCurrentCellState ( ) ;
1075
1071
}
1076
1072
1077
- if ( slot is { } )
1073
+ if ( newSlot . HasValue )
1078
1074
{
1079
- var cell = await ScrollCellIntoView ( slot . Value ) ;
1075
+ var cell = await ScrollCellIntoView ( newSlot . Value ) ;
1080
1076
cell ? . ApplyCurrentCellState ( ) ;
1081
1077
}
1082
-
1083
- CurrentCellChanged ? . Invoke ( this , new TableViewCurrentCellChangedEventArgs ( oldSlot , slot ) ) ;
1084
1078
}
1085
1079
1086
1080
/// <summary>
@@ -1101,10 +1095,24 @@ private void OnCellSelectionChanged()
1101
1095
row ? . ApplyCellsSelectionState ( ) ;
1102
1096
}
1103
1097
1104
- SelectedCellsChanged ? . Invoke ( this , new TableViewCellSelectionChangedEvenArgs ( oldSelection , SelectedCells ) ) ;
1098
+ InvokeCellSelectionChangedEvent ( oldSelection ) ;
1105
1099
} ) ;
1106
1100
}
1107
1101
1102
+ /// <summary>
1103
+ /// Invokes the <see cref="CellSelectionChanged"/> event to notify subscribers of changes in the selected cells.
1104
+ /// </summary>
1105
+ private void InvokeCellSelectionChangedEvent ( HashSet < TableViewCellSlot > oldSelection )
1106
+ {
1107
+ var removedCells = oldSelection . Except ( SelectedCells ) . ToList ( ) ;
1108
+ var addedCells = SelectedCells . Except ( oldSelection ) . ToList ( ) ;
1109
+
1110
+ if ( removedCells . Count > 0 || addedCells . Count > 0 )
1111
+ {
1112
+ CellSelectionChanged ? . Invoke ( this , new TableViewCellSelectionChangedEventArgs ( removedCells , addedCells ) ) ;
1113
+ }
1114
+ }
1115
+
1108
1116
/// <summary>
1109
1117
/// Scrolls the specified cell slot into view.
1110
1118
/// </summary>
@@ -1227,9 +1235,9 @@ void ViewChanged(object? _, ScrollViewerViewChangedEventArgs e)
1227
1235
/// <summary>
1228
1236
/// Gets the cell based on the specified cell slot.
1229
1237
/// </summary>
1230
- internal TableViewCell GetCellFromSlot ( TableViewCellSlot slot )
1238
+ internal TableViewCell ? GetCellFromSlot ( TableViewCellSlot slot )
1231
1239
{
1232
- return slot . IsValid ( this ) && ContainerFromIndex ( slot . Row ) is TableViewRow row ? row . Cells [ slot . Column ] : default ! ;
1240
+ return slot . IsValid ( this ) && ContainerFromIndex ( slot . Row ) is TableViewRow row ? row . Cells [ slot . Column ] : default ;
1233
1241
}
1234
1242
1235
1243
/// <summary>
@@ -1475,12 +1483,12 @@ public virtual void OnClearSorting(TableViewClearSortingEventArgs eventArgs)
1475
1483
public event EventHandler < TableViewClearSortingEventArgs > ? ClearSorting ;
1476
1484
1477
1485
/// <summary>
1478
- /// Internal event triggered when selected cells change.
1486
+ /// Event triggered when selected cells change.
1479
1487
/// </summary>
1480
- internal event EventHandler < TableViewCellSelectionChangedEvenArgs > ? SelectedCellsChanged ;
1488
+ public event EventHandler < TableViewCellSelectionChangedEventArgs > ? CellSelectionChanged ;
1481
1489
1482
1490
/// <summary>
1483
- /// Internal event triggered when the current cell changes.
1491
+ /// Event triggered when the current cell changes.
1484
1492
/// </summary>
1485
- internal event EventHandler < TableViewCurrentCellChangedEventArgs > ? CurrentCellChanged ;
1493
+ public event DependencyPropertyChangedEventHandler ? CurrentCellChanged ;
1486
1494
}
0 commit comments