Skip to content

Commit 4c690d4

Browse files
authored
Merge pull request AvaloniaUI#3444 from MarchingCube/fix-selecting-items-control-init
Fix SelectingItemsControl init behavior
2 parents 155e9d4 + a238fd3 commit 4c690d4

2 files changed

Lines changed: 44 additions & 11 deletions

File tree

src/Avalonia.Controls/Primitives/SelectingItemsControl.cs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections;
66
using System.Collections.Generic;
77
using System.Collections.Specialized;
8+
using System.Diagnostics;
89
using System.Linq;
910
using Avalonia.Collections;
1011
using Avalonia.Controls.Generators;
@@ -240,17 +241,14 @@ protected SelectionMode SelectionMode
240241
public override void BeginInit()
241242
{
242243
base.BeginInit();
243-
++_updateCount;
244-
_updateSelectedIndex = int.MinValue;
244+
245+
InternalBeginInit();
245246
}
246247

247248
/// <inheritdoc/>
248249
public override void EndInit()
249250
{
250-
if (--_updateCount == 0)
251-
{
252-
UpdateFinished();
253-
}
251+
InternalEndInit();
254252

255253
base.EndInit();
256254
}
@@ -437,18 +435,16 @@ protected override void OnContainersRecycled(ItemContainerEventArgs e)
437435
protected override void OnDataContextBeginUpdate()
438436
{
439437
base.OnDataContextBeginUpdate();
440-
++_updateCount;
438+
439+
InternalBeginInit();
441440
}
442441

443442
/// <inheritdoc/>
444443
protected override void OnDataContextEndUpdate()
445444
{
446445
base.OnDataContextEndUpdate();
447446

448-
if (--_updateCount == 0)
449-
{
450-
UpdateFinished();
451-
}
447+
InternalEndInit();
452448
}
453449

454450
protected override void OnKeyDown(KeyEventArgs e)
@@ -1118,6 +1114,26 @@ private void UpdateFinished()
11181114
}
11191115
}
11201116

1117+
private void InternalBeginInit()
1118+
{
1119+
if (_updateCount == 0)
1120+
{
1121+
_updateSelectedIndex = int.MinValue;
1122+
}
1123+
1124+
++_updateCount;
1125+
}
1126+
1127+
private void InternalEndInit()
1128+
{
1129+
Debug.Assert(_updateCount > 0);
1130+
1131+
if (--_updateCount == 0)
1132+
{
1133+
UpdateFinished();
1134+
}
1135+
}
1136+
11211137
private class Selection : IEnumerable<int>
11221138
{
11231139
private readonly List<int> _list = new List<int>();

tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,23 @@ public void SelectedIndex_Should_Be_Minus_1_After_Initialize()
129129
Assert.Equal(-1, target.SelectedIndex);
130130
}
131131

132+
[Fact]
133+
public void SelectedIndex_Should_Be_Minus_1_Without_Initialize()
134+
{
135+
var items = new[]
136+
{
137+
new Item(),
138+
new Item(),
139+
};
140+
141+
var target = new ListBox();
142+
target.Items = items;
143+
target.Template = Template();
144+
target.DataContext = new object();
145+
146+
Assert.Equal(-1, target.SelectedIndex);
147+
}
148+
132149
[Fact]
133150
public void SelectedIndex_Should_Be_0_After_Initialize_With_AlwaysSelected()
134151
{

0 commit comments

Comments
 (0)