From 81b7b5438650896b2655fd03b423c19479f9935a Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Tue, 25 Aug 2020 12:40:06 +0200 Subject: [PATCH] Auto-select when items added. --- .../Primitives/SelectingItemsControl.cs | 31 ++++++------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs b/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs index f6c2d00d37..6a17fefc2c 100644 --- a/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs +++ b/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Collections.Specialized; using System.ComponentModel; using System.Linq; using Avalonia.Controls.Generators; @@ -274,27 +275,14 @@ namespace Avalonia.Controls.Primitives return null; } - /// - protected override void ItemsChanged(AvaloniaPropertyChangedEventArgs e) + protected override void ItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { - base.ItemsChanged(e); - - //if (_updateCount == 0) - //{ - // var newIndex = -1; + base.ItemsCollectionChanged(sender, e); - // if (SelectedIndex != -1) - // { - // newIndex = IndexOf((IEnumerable)e.NewValue, SelectedItem); - // } - - // if (AlwaysSelected && Items != null && Items.Cast().Any()) - // { - // newIndex = 0; - // } - - // SelectedIndex = newIndex; - //} + if (AlwaysSelected && SelectedIndex == -1 && ItemCount > 0) + { + SelectedIndex = 0; + } } /// @@ -309,9 +297,10 @@ namespace Avalonia.Controls.Primitives Selection.Select(container.Index); MarkContainerSelected(container.ContainerControl, true); } - else if (Selection.IsSelected(container.Index) == true) + else { - MarkContainerSelected(container.ContainerControl, true); + var selected = Selection.IsSelected(container.Index); + MarkContainerSelected(container.ContainerControl, selected); } } }