Browse Source

Merge pull request #3444 from MarchingCube/fix-selecting-items-control-init

Fix SelectingItemsControl init behavior
release/0.9.2
Steven Kirk 7 years ago
committed by Dan Walmsley
parent
commit
0a80b02539
  1. 38
      src/Avalonia.Controls/Primitives/SelectingItemsControl.cs
  2. 17
      tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests.cs

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

@ -5,6 +5,7 @@ using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq; using System.Linq;
using Avalonia.Collections; using Avalonia.Collections;
using Avalonia.Controls.Generators; using Avalonia.Controls.Generators;
@ -241,17 +242,14 @@ namespace Avalonia.Controls.Primitives
public override void BeginInit() public override void BeginInit()
{ {
base.BeginInit(); base.BeginInit();
++_updateCount;
_updateSelectedIndex = int.MinValue; InternalBeginInit();
} }
/// <inheritdoc/> /// <inheritdoc/>
public override void EndInit() public override void EndInit()
{ {
if (--_updateCount == 0) InternalEndInit();
{
UpdateFinished();
}
base.EndInit(); base.EndInit();
} }
@ -429,7 +427,8 @@ namespace Avalonia.Controls.Primitives
protected override void OnDataContextBeginUpdate() protected override void OnDataContextBeginUpdate()
{ {
base.OnDataContextBeginUpdate(); base.OnDataContextBeginUpdate();
++_updateCount;
InternalBeginInit();
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -437,10 +436,7 @@ namespace Avalonia.Controls.Primitives
{ {
base.OnDataContextEndUpdate(); base.OnDataContextEndUpdate();
if (--_updateCount == 0) InternalEndInit();
{
UpdateFinished();
}
} }
protected override void OnKeyDown(KeyEventArgs e) protected override void OnKeyDown(KeyEventArgs e)
@ -1110,6 +1106,26 @@ namespace Avalonia.Controls.Primitives
} }
} }
private void InternalBeginInit()
{
if (_updateCount == 0)
{
_updateSelectedIndex = int.MinValue;
}
++_updateCount;
}
private void InternalEndInit()
{
Debug.Assert(_updateCount > 0);
if (--_updateCount == 0)
{
UpdateFinished();
}
}
private class Selection : IEnumerable<int> private class Selection : IEnumerable<int>
{ {
private readonly List<int> _list = new List<int>(); private readonly List<int> _list = new List<int>();

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

@ -129,6 +129,23 @@ namespace Avalonia.Controls.UnitTests.Primitives
Assert.Equal(-1, target.SelectedIndex); Assert.Equal(-1, target.SelectedIndex);
} }
[Fact]
public void SelectedIndex_Should_Be_Minus_1_Without_Initialize()
{
var items = new[]
{
new Item(),
new Item(),
};
var target = new ListBox();
target.Items = items;
target.Template = Template();
target.DataContext = new object();
Assert.Equal(-1, target.SelectedIndex);
}
[Fact] [Fact]
public void SelectedIndex_Should_Be_0_After_Initialize_With_AlwaysSelected() public void SelectedIndex_Should_Be_0_After_Initialize_With_AlwaysSelected()
{ {

Loading…
Cancel
Save