Skip to content

Commit

Permalink
Merge branch 'master' into improve-togglebutton
Browse files Browse the repository at this point in the history
  • Loading branch information
grokys authored Jan 17, 2020
2 parents 3cdc9c9 + 4c690d4 commit 9953bdd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
38 changes: 27 additions & 11 deletions src/Avalonia.Controls/Primitives/SelectingItemsControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using Avalonia.Collections;
using Avalonia.Controls.Generators;
Expand Down Expand Up @@ -240,17 +241,14 @@ protected SelectionMode SelectionMode
public override void BeginInit()
{
base.BeginInit();
++_updateCount;
_updateSelectedIndex = int.MinValue;

InternalBeginInit();
}

/// <inheritdoc/>
public override void EndInit()
{
if (--_updateCount == 0)
{
UpdateFinished();
}
InternalEndInit();

base.EndInit();
}
Expand Down Expand Up @@ -437,18 +435,16 @@ protected override void OnContainersRecycled(ItemContainerEventArgs e)
protected override void OnDataContextBeginUpdate()
{
base.OnDataContextBeginUpdate();
++_updateCount;

InternalBeginInit();
}

/// <inheritdoc/>
protected override void OnDataContextEndUpdate()
{
base.OnDataContextEndUpdate();

if (--_updateCount == 0)
{
UpdateFinished();
}
InternalEndInit();
}

protected override void OnKeyDown(KeyEventArgs e)
Expand Down Expand Up @@ -1118,6 +1114,26 @@ private void UpdateFinished()
}
}

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 readonly List<int> _list = new List<int>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,23 @@ public void SelectedIndex_Should_Be_Minus_1_After_Initialize()
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]
public void SelectedIndex_Should_Be_0_After_Initialize_With_AlwaysSelected()
{
Expand Down

0 comments on commit 9953bdd

Please sign in to comment.