From bcbe9c3dbd6e2eb824b9947d667096d98edbaca5 Mon Sep 17 00:00:00 2001 From: Benedikt Schroeder Date: Wed, 18 Sep 2019 19:01:48 +0200 Subject: [PATCH] Fix setting SelectedIndex in XAML --- .../Primitives/SelectingItemsControl.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs b/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs index cc0c5f52be..761a3f3b3d 100644 --- a/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs +++ b/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs @@ -304,6 +304,11 @@ namespace Avalonia.Controls.Primitives { base.ItemsCollectionChanged(sender, e); + if (_updateCount > 0) + { + return; + } + switch (e.Action) { case NotifyCollectionChangedAction.Add: @@ -1071,13 +1076,16 @@ namespace Avalonia.Controls.Primitives private void UpdateFinished() { - if (_updateSelectedIndex != int.MinValue) + if (_updateSelectedItem != null) { - SelectedIndex = _updateSelectedIndex; + SelectedItem = _updateSelectedItem; } - else if (_updateSelectedItem != null) + else { - SelectedItem = _updateSelectedItem; + if (ItemCount > 0) + { + SelectedIndex = _updateSelectedIndex != int.MinValue ? _updateSelectedIndex : 0; + } } }