Skip to content

Commit

Permalink
Unify SelectingItemsControl init handling and add extra checks to ens…
Browse files Browse the repository at this point in the history
…ure correctness.
  • Loading branch information
MarchingCube committed Jan 17, 2020
1 parent 1e673ec commit a238fd3
Showing 1 changed file with 27 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

0 comments on commit a238fd3

Please sign in to comment.