From 0bbb66eeebe26975233f8ec74b8b615d2e0b5979 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 8 Mar 2023 11:01:09 +0100 Subject: [PATCH] Fix failing selection tests. --- .../Primitives/SelectingItemsControl.cs | 16 ++++++----- .../Selection/InternalSelectionModel.cs | 27 ++++++++++++++++++- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs b/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs index a31472fab4..cb4efb344f 100644 --- a/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs +++ b/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs @@ -1232,16 +1232,18 @@ namespace Avalonia.Controls.Primitives Selection = state.Selection.Value; } - if (state.SelectedItems.HasValue) + if (_selection is InternalSelectionModel s) { - SelectedItems = state.SelectedItems.Value; + s.Update(ItemsView.Source, state.SelectedItems); } - - Selection.Source = ItemsView.Source; - - if (ItemsView.Count == 0) + else { - Selection.Clear(); + if (state.SelectedItems.HasValue) + { + SelectedItems = state.SelectedItems.Value; + } + + Selection.Source = ItemsView.Source; } if (state.SelectedValue.HasValue) diff --git a/src/Avalonia.Controls/Selection/InternalSelectionModel.cs b/src/Avalonia.Controls/Selection/InternalSelectionModel.cs index d0e6144f59..c8ad9bd88b 100644 --- a/src/Avalonia.Controls/Selection/InternalSelectionModel.cs +++ b/src/Avalonia.Controls/Selection/InternalSelectionModel.cs @@ -5,6 +5,7 @@ using System.Collections.Specialized; using System.Diagnostics.CodeAnalysis; using System.Linq; using Avalonia.Collections; +using Avalonia.Data; namespace Avalonia.Controls.Selection { @@ -13,6 +14,7 @@ namespace Avalonia.Controls.Selection private IList? _writableSelectedItems; private int _ignoreModelChanges; private bool _ignoreSelectedItemsChanges; + private bool _skipSyncFromSelectedItems; private bool _isResetting; public InternalSelectionModel() @@ -60,6 +62,29 @@ namespace Avalonia.Controls.Selection } } + internal void Update(IEnumerable? source, Optional selectedItems) + { + var previousSource = Source; + var previousWritableSelectedItems = _writableSelectedItems; + + try + { + _skipSyncFromSelectedItems = true; + SetSource(source); + if (selectedItems.HasValue) + WritableSelectedItems = selectedItems.Value; + } + finally + { + _skipSyncFromSelectedItems = false; + } + + // We skipped the sync from WritableSelectedItems before; do it now that both + // the source and WritableSelectedItems are updated. + if (previousSource != Source || previousWritableSelectedItems != _writableSelectedItems) + SyncFromSelectedItems(); + } + private protected override void SetSource(IEnumerable? value) { if (Source == value) @@ -121,7 +146,7 @@ namespace Avalonia.Controls.Selection private void SyncFromSelectedItems() { - if (Source is null || _writableSelectedItems is null) + if (_skipSyncFromSelectedItems || Source is null || _writableSelectedItems is null) { return; }