Browse Source

Prevent nested selection changing parent selection.

Added previously failing
Nested_ListBox_Does_Not_Change_Parent_SelectedIndex test to check it.
pull/508/head
Steven Kirk 11 years ago
parent
commit
004ed96de5
  1. 4
      src/Perspex.Controls/Presenters/CarouselPresenter.cs
  2. 15
      src/Perspex.Controls/Primitives/SelectingItemsControl.cs
  3. 35
      tests/Perspex.Controls.UnitTests/Primitives/SelectingItemsControlTests.cs

4
src/Perspex.Controls/Presenters/CarouselPresenter.cs

@ -1,16 +1,12 @@
// Copyright (c) The Perspex Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using Perspex.Animation;
using Perspex.Controls.Generators;
using Perspex.Controls.Primitives;
using Perspex.Controls.Templates;
using Perspex.Controls.Utils;
using Perspex.Data;

15
src/Perspex.Controls/Primitives/SelectingItemsControl.cs

@ -574,13 +574,22 @@ namespace Perspex.Controls.Primitives
{
if (!_ignoreContainerSelectionChanged)
{
var selectable = (ISelectable)e.Source;
var control = e.Source as IControl;
var selectable = e.Source as ISelectable;
if (selectable != null)
if (control != null &&
selectable != null &&
control.LogicalParent == this &&
ItemContainerGenerator?.IndexFromContainer(control) != -1)
{
UpdateSelectionFromEventSource(e.Source, selectable.IsSelected);
UpdateSelection(control, selectable.IsSelected);
}
}
if (e.Source != this)
{
e.Handled = true;
}
}
/// <summary>

35
tests/Perspex.Controls.UnitTests/Primitives/SelectingItemsControlTests.cs

@ -570,6 +570,41 @@ namespace Perspex.Controls.UnitTests.Primitives
Assert.Same(vm.Child.SelectedItem, target.SelectedItem);
}
[Fact]
public void Nested_ListBox_Does_Not_Change_Parent_SelectedIndex()
{
SelectingItemsControl nested;
var root = new SelectingItemsControl
{
Template = Template(),
Items = new IControl[]
{
new Border(),
nested = new ListBox
{
Template = Template(),
Items = new[] { "foo", "bar" },
SelectedIndex = 1,
}
},
SelectedIndex = 0,
};
root.ApplyTemplate();
root.Presenter.ApplyTemplate();
nested.ApplyTemplate();
nested.Presenter.ApplyTemplate();
Assert.Equal(0, root.SelectedIndex);
Assert.Equal(1, nested.SelectedIndex);
nested.SelectedIndex = 0;
Assert.Equal(0, root.SelectedIndex);
}
private FuncControlTemplate Template()
{
return new FuncControlTemplate<SelectingItemsControl>(control =>

Loading…
Cancel
Save