Browse Source

Fix failing selection tests.

pull/10590/head
Steven Kirk 4 years ago
parent
commit
0bbb66eeeb
  1. 16
      src/Avalonia.Controls/Primitives/SelectingItemsControl.cs
  2. 27
      src/Avalonia.Controls/Selection/InternalSelectionModel.cs

16
src/Avalonia.Controls/Primitives/SelectingItemsControl.cs

@ -1232,16 +1232,18 @@ namespace Avalonia.Controls.Primitives
Selection = state.Selection.Value; Selection = state.Selection.Value;
} }
if (state.SelectedItems.HasValue) if (_selection is InternalSelectionModel s)
{ {
SelectedItems = state.SelectedItems.Value; s.Update(ItemsView.Source, state.SelectedItems);
} }
else
Selection.Source = ItemsView.Source;
if (ItemsView.Count == 0)
{ {
Selection.Clear(); if (state.SelectedItems.HasValue)
{
SelectedItems = state.SelectedItems.Value;
}
Selection.Source = ItemsView.Source;
} }
if (state.SelectedValue.HasValue) if (state.SelectedValue.HasValue)

27
src/Avalonia.Controls/Selection/InternalSelectionModel.cs

@ -5,6 +5,7 @@ using System.Collections.Specialized;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using Avalonia.Collections; using Avalonia.Collections;
using Avalonia.Data;
namespace Avalonia.Controls.Selection namespace Avalonia.Controls.Selection
{ {
@ -13,6 +14,7 @@ namespace Avalonia.Controls.Selection
private IList? _writableSelectedItems; private IList? _writableSelectedItems;
private int _ignoreModelChanges; private int _ignoreModelChanges;
private bool _ignoreSelectedItemsChanges; private bool _ignoreSelectedItemsChanges;
private bool _skipSyncFromSelectedItems;
private bool _isResetting; private bool _isResetting;
public InternalSelectionModel() public InternalSelectionModel()
@ -60,6 +62,29 @@ namespace Avalonia.Controls.Selection
} }
} }
internal void Update(IEnumerable? source, Optional<IList?> 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) private protected override void SetSource(IEnumerable? value)
{ {
if (Source == value) if (Source == value)
@ -121,7 +146,7 @@ namespace Avalonia.Controls.Selection
private void SyncFromSelectedItems() private void SyncFromSelectedItems()
{ {
if (Source is null || _writableSelectedItems is null) if (_skipSyncFromSelectedItems || Source is null || _writableSelectedItems is null)
{ {
return; return;
} }

Loading…
Cancel
Save