Browse Source

Handle deselecting removed items...

in SelectingItemsControl.
pull/58/head
Steven Kirk 11 years ago
parent
commit
d27205716a
  1. 2
      Perspex.Controls/ItemsControl.cs
  2. 38
      Perspex.Controls/Primitives/SelectingItemsControl.cs
  3. 5
      Perspex.Controls/Utils/IEnumerableUtils.cs
  4. 55
      Tests/Perspex.Controls.UnitTests/Primitives/SelectingItemsControlTests.cs

2
Perspex.Controls/ItemsControl.cs

@ -126,7 +126,7 @@ namespace Perspex.Controls
if (incc != null)
{
incc.CollectionChanged -= this.ItemsCollectionChanged;
incc.CollectionChanged += this.ItemsCollectionChanged;
}
}

38
Perspex.Controls/Primitives/SelectingItemsControl.cs

@ -11,6 +11,8 @@ namespace Perspex.Controls.Primitives
using Perspex.VisualTree;
using System;
using System.Linq;
using System.Collections;
using System.Collections.Specialized;
public abstract class SelectingItemsControl : ItemsControl
{
@ -66,6 +68,42 @@ namespace Perspex.Controls.Primitives
set { this.SetValue(SelectedItemProperty, value); }
}
protected override void ItemsChanged(IEnumerable oldValue, IEnumerable newValue)
{
base.ItemsChanged(oldValue, newValue);
var selected = this.SelectedItem;
if (selected != null)
{
if (newValue == null || !newValue.Contains(selected))
{
this.SelectedItem = null;
}
}
}
protected override void ItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
base.ItemsCollectionChanged(sender, e);
var selected = this.SelectedItem;
switch (e.Action)
{
case NotifyCollectionChangedAction.Remove:
case NotifyCollectionChangedAction.Reset:
if (e.OldItems.Contains(selected))
{
this.SelectedItem = null;
}
break;
case NotifyCollectionChangedAction.Move:
this.SelectedItem = this.Items.IndexOf(selected);
break;
}
}
protected virtual void MoveSelection(FocusNavigationDirection direction)
{
var panel = this.Presenter?.Panel as INavigablePanel;

5
Perspex.Controls/Utils/IEnumerableUtils.cs

@ -13,6 +13,11 @@ namespace Perspex.Controls.Utils
internal static class IEnumerableUtils
{
public static bool Contains(this IEnumerable items, object item)
{
return items.IndexOf(item) != -1;
}
public static int Count(this IEnumerable items)
{
Contract.Requires<ArgumentNullException>(items != null);

55
Tests/Perspex.Controls.UnitTests/Primitives/SelectingItemsControlTests.cs

@ -6,6 +6,7 @@
namespace Perspex.Controls.Primitives.UnitTests
{
using Perspex.Collections;
using Perspex.Controls.Presenters;
using Perspex.Controls.Primitives;
using Perspex.Input;
@ -173,6 +174,60 @@ namespace Perspex.Controls.Primitives.UnitTests
target.SelectedItem = new Item();
}
[Fact]
public void Clearing_Items_Should_Clear_Selection()
{
var items = new PerspexList<Item>
{
new Item(),
new Item(),
};
var target = new Target
{
Items = items,
Template = this.Template(),
};
target.ApplyTemplate();
target.SelectedIndex = 1;
Assert.Equal(items[1], target.SelectedItem);
Assert.Equal(1, target.SelectedIndex);
target.Items = null;
Assert.Equal(null, target.SelectedItem);
Assert.Equal(-1, target.SelectedIndex);
}
[Fact]
public void Removing_Selected_Item_Should_Clear_Selection()
{
var items = new PerspexList<Item>
{
new Item(),
new Item(),
};
var target = new Target
{
Items = items,
Template = this.Template(),
};
target.ApplyTemplate();
target.SelectedIndex = 1;
Assert.Equal(items[1], target.SelectedItem);
Assert.Equal(1, target.SelectedIndex);
items.RemoveAt(1);
Assert.Equal(null, target.SelectedItem);
Assert.Equal(-1, target.SelectedIndex);
}
[Fact]
public void PointerPressed_Event_Should_Be_Handled()
{

Loading…
Cancel
Save