Browse Source
Merge pull request #1763 from AvaloniaUI/fixes/carousel-presenter-fix
Fix SelectingItemsControl
pull/1767/head
Jumar Macato
8 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with
34 additions and
1 deletions
-
src/Avalonia.Controls/Primitives/SelectingItemsControl.cs
-
tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests.cs
|
|
|
@ -360,7 +360,7 @@ namespace Avalonia.Controls.Primitives |
|
|
|
{ |
|
|
|
if (!AlwaysSelected) |
|
|
|
{ |
|
|
|
SelectedIndex = -1; |
|
|
|
selectedIndex = SelectedIndex = -1; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
@ -368,6 +368,11 @@ namespace Avalonia.Controls.Primitives |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
var items = Items?.Cast<object>(); |
|
|
|
if (selectedIndex >= items.Count()) |
|
|
|
{ |
|
|
|
selectedIndex = SelectedIndex = items.Count() - 1; |
|
|
|
} |
|
|
|
break; |
|
|
|
|
|
|
|
case NotifyCollectionChangedAction.Reset: |
|
|
|
|
|
|
|
@ -149,6 +149,34 @@ namespace Avalonia.Controls.UnitTests.Primitives |
|
|
|
Assert.Equal(1, target.SelectedIndex); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void SelectedIndex_Item_Is_Updated_As_Items_Removed_When_Last_Item_Is_Selected() |
|
|
|
{ |
|
|
|
var items = new ObservableCollection<string> |
|
|
|
{ |
|
|
|
"Foo", |
|
|
|
"Bar", |
|
|
|
"FooBar" |
|
|
|
}; |
|
|
|
|
|
|
|
var target = new SelectingItemsControl |
|
|
|
{ |
|
|
|
Items = items, |
|
|
|
Template = Template(), |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
target.SelectedItem = items[2]; |
|
|
|
|
|
|
|
Assert.Equal(items[2], target.SelectedItem); |
|
|
|
Assert.Equal(2, target.SelectedIndex); |
|
|
|
|
|
|
|
items.RemoveAt(0); |
|
|
|
|
|
|
|
Assert.Equal(items[1], target.SelectedItem); |
|
|
|
Assert.Equal(1, target.SelectedIndex); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Setting_SelectedItem_To_Not_Present_Item_Should_Clear_Selection() |
|
|
|
{ |
|
|
|
|