Skip to content

Commit 21afb4f

Browse files
authored
Merge pull request #389 from Mangepange/bugfix_381_isreadonly_editing
#381 End current edit when IsReadOnly is set to false
2 parents 254ed49 + c80cb49 commit 21afb4f

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

src/Columns/TableViewColumn.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -508,8 +508,6 @@ private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChan
508508
column.OwningCollection.HandleColumnPropertyChanged(column, nameof(MaxWidth));
509509
else if (e.Property == ActualWidthProperty)
510510
column.OwningCollection.HandleColumnPropertyChanged(column, nameof(ActualWidth));
511-
else if (e.Property == IsReadOnlyProperty)
512-
column.OwningCollection.HandleColumnPropertyChanged(column, nameof(IsReadOnly));
513511
else if (e.Property == VisibilityProperty)
514512
column.OwningCollection.HandleColumnPropertyChanged(column, nameof(Visibility));
515513
else if (e.Property == OrderProperty)
@@ -528,6 +526,27 @@ private static void OnColumnAutoWidthModeChanged(DependencyObject d, DependencyP
528526
}
529527
}
530528

529+
/// <summary>
530+
/// Handles changes to the IsReadOnly property.
531+
/// </summary>
532+
private static void OnIsReadOnlyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
533+
{
534+
if (d is TableViewColumn column)
535+
{
536+
if (column.TableView is TableView tableView &&
537+
tableView.IsReadOnly &&
538+
tableView.IsEditing &&
539+
tableView.CurrentCellSlot is not null &&
540+
tableView.GetCellFromSlot(tableView.CurrentCellSlot.Value) is { } currentCell &&
541+
tableView.EndCellEditing(TableViewEditAction.Cancel, currentCell))
542+
{
543+
tableView.SetIsEditing(false);
544+
}
545+
546+
column.OwningCollection?.HandleColumnPropertyChanged(column, nameof(IsReadOnly));
547+
}
548+
}
549+
531550
/// <summary>
532551
/// Handles changes to the CanFilter property.
533552
/// </summary>
@@ -612,7 +631,7 @@ public string? SortMemberPath
612631
/// <summary>
613632
/// Identifies the IsReadOnly dependency property.
614633
/// </summary>
615-
public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register(nameof(IsReadOnly), typeof(bool), typeof(TableViewColumn), new PropertyMetadata(false, OnPropertyChanged));
634+
public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register(nameof(IsReadOnly), typeof(bool), typeof(TableViewColumn), new PropertyMetadata(false, OnIsReadOnlyChanged));
616635

617636
/// <summary>
618637
/// Identifies the Visibility dependency property.

src/TableView.Properties.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -957,9 +957,18 @@ private static void OnIsReadOnlyChanged(DependencyObject d, DependencyPropertyCh
957957
{
958958
tableView.OnIsReadOnlyChanged(e);
959959

960-
if ((tableView.SelectionMode is ListViewSelectionMode.None
960+
if (!tableView.IsReadOnly) return;
961+
962+
if (tableView.IsEditing &&
963+
tableView.CurrentCellSlot is not null &&
964+
tableView.GetCellFromSlot(tableView.CurrentCellSlot.Value) is { } currentCell &&
965+
tableView.EndCellEditing(TableViewEditAction.Cancel, currentCell))
966+
{
967+
tableView.SetIsEditing(false);
968+
}
969+
970+
if (tableView.SelectionMode is ListViewSelectionMode.None
961971
|| tableView.SelectionUnit is TableViewSelectionUnit.Row)
962-
&& tableView.IsReadOnly)
963972
{
964973
tableView.CurrentCellSlot = null;
965974
}

0 commit comments

Comments
 (0)