Browse Source

Merge branch 'master' into issues/1998

pull/2002/head
danwalmsley 8 years ago
committed by GitHub
parent
commit
ee741e243a
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      src/Avalonia.Controls/Primitives/SelectingItemsControl.cs
  2. 42
      tests/Avalonia.Controls.UnitTests/ListBoxTests.cs

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

@ -62,7 +62,7 @@ namespace Avalonia.Controls.Primitives
AvaloniaProperty.RegisterDirect<SelectingItemsControl, object>( AvaloniaProperty.RegisterDirect<SelectingItemsControl, object>(
nameof(SelectedItem), nameof(SelectedItem),
o => o.SelectedItem, o => o.SelectedItem,
(o, v) => o.SelectedItem = v, (o, v) => o.SelectedItem = v,
defaultBindingMode: BindingMode.TwoWay); defaultBindingMode: BindingMode.TwoWay);
/// <summary> /// <summary>
@ -88,7 +88,7 @@ namespace Avalonia.Controls.Primitives
/// </summary> /// </summary>
public static readonly RoutedEvent<RoutedEventArgs> IsSelectedChangedEvent = public static readonly RoutedEvent<RoutedEventArgs> IsSelectedChangedEvent =
RoutedEvent.Register<SelectingItemsControl, RoutedEventArgs>( RoutedEvent.Register<SelectingItemsControl, RoutedEventArgs>(
"IsSelectedChanged", "IsSelectedChanged",
RoutingStrategies.Bubble); RoutingStrategies.Bubble);
/// <summary> /// <summary>
@ -96,7 +96,7 @@ namespace Avalonia.Controls.Primitives
/// </summary> /// </summary>
public static readonly RoutedEvent<SelectionChangedEventArgs> SelectionChangedEvent = public static readonly RoutedEvent<SelectionChangedEventArgs> SelectionChangedEvent =
RoutedEvent.Register<SelectingItemsControl, SelectionChangedEventArgs>( RoutedEvent.Register<SelectingItemsControl, SelectionChangedEventArgs>(
"SelectionChanged", "SelectionChanged",
RoutingStrategies.Bubble); RoutingStrategies.Bubble);
private static readonly IList Empty = new object[0]; private static readonly IList Empty = new object[0];
@ -521,7 +521,7 @@ namespace Avalonia.Controls.Primitives
else if (multi && range) else if (multi && range)
{ {
SynchronizeItems( SynchronizeItems(
SelectedItems, SelectedItems,
GetRange(Items, SelectedIndex, index)); GetRange(Items, SelectedIndex, index));
} }
else else
@ -583,7 +583,7 @@ namespace Avalonia.Controls.Primitives
} }
/// <summary> /// <summary>
/// Updates the selection based on an event that may have originated in a container that /// Updates the selection based on an event that may have originated in a container that
/// belongs to the control. /// belongs to the control.
/// </summary> /// </summary>
/// <param name="eventSource">The control that raised the event.</param> /// <param name="eventSource">The control that raised the event.</param>
@ -595,7 +595,7 @@ namespace Avalonia.Controls.Primitives
/// false. /// false.
/// </returns> /// </returns>
protected bool UpdateSelectionFromEventSource( protected bool UpdateSelectionFromEventSource(
IInteractive eventSource, IInteractive eventSource,
bool select = true, bool select = true,
bool rangeModifier = false, bool rangeModifier = false,
bool toggleModifier = false) bool toggleModifier = false)
@ -807,12 +807,10 @@ namespace Avalonia.Controls.Primitives
SelectedIndex = -1; SelectedIndex = -1;
} }
} }
else
foreach (var item in e.OldItems)
{ {
foreach (var item in e.OldItems) MarkItemSelected(item, false);
{
MarkItemSelected(item, false);
}
} }
removed = e.OldItems; removed = e.OldItems;

42
tests/Avalonia.Controls.UnitTests/ListBoxTests.cs

@ -1,13 +1,11 @@
// Copyright (c) The Avalonia Project. All rights reserved. // Copyright (c) The Avalonia Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information. // Licensed under the MIT license. See licence.md file in the project root for full license information.
using System;
using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using Avalonia.Collections; using Avalonia.Collections;
using Avalonia.Controls.Presenters; using Avalonia.Controls.Presenters;
using Avalonia.Controls.Templates; using Avalonia.Controls.Templates;
using Avalonia.Data; using Avalonia.Input;
using Avalonia.LogicalTree; using Avalonia.LogicalTree;
using Avalonia.Styling; using Avalonia.Styling;
using Avalonia.UnitTests; using Avalonia.UnitTests;
@ -196,6 +194,44 @@ namespace Avalonia.Controls.UnitTests
target.Presenter.Panel.Children.Cast<ListBoxItem>().Select(x => (string)x.Content)); target.Presenter.Panel.Children.Cast<ListBoxItem>().Select(x => (string)x.Content));
} }
[Fact]
public void Toggle_Selection_Should_Update_Containers()
{
var items = Enumerable.Range(0, 10).Select(x => $"Item {x}").ToArray();
var target = new ListBox
{
Template = ListBoxTemplate(),
Items = items,
SelectionMode = SelectionMode.Toggle,
ItemTemplate = new FuncDataTemplate<string>(x => new TextBlock { Height = 10 })
};
Prepare(target);
var lbItems = target.GetLogicalChildren().OfType<ListBoxItem>().ToArray();
var item = lbItems[0];
Assert.Equal(false, item.IsSelected);
RaisePressedEvent(target, item, MouseButton.Left);
Assert.Equal(true, item.IsSelected);
RaisePressedEvent(target, item, MouseButton.Left);
Assert.Equal(false, item.IsSelected);
}
private void RaisePressedEvent(ListBox listBox, ListBoxItem item, MouseButton mouseButton)
{
listBox.RaiseEvent(new PointerPressedEventArgs
{
Source = item,
RoutedEvent = InputElement.PointerPressedEvent,
MouseButton = mouseButton
});
}
[Fact] [Fact]
public void ListBox_After_Scroll_IndexOutOfRangeException_Shouldnt_Be_Thrown() public void ListBox_After_Scroll_IndexOutOfRangeException_Shouldnt_Be_Thrown()

Loading…
Cancel
Save