Skip to content

Commit

Permalink
fix: Changing Pivot.SelectedItem should reflect on Pivot.SelectedIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Sep 7, 2021
1 parent 5a85199 commit 166e7a7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,29 @@ public async Task Check_Changing_Header_Affects_UI()
headerItem.Content.Should().Be("New text");
}

[TestMethod]
[RunsOnUIThread]
public async Task Check_Changing_SelectedItem_Affects_SelectedIndex()
{
var pivotItem1 = new PivotItem { Header = "First" };
var pivotItem2 = new PivotItem { Header = "Second" };
var SUT = new Pivot
{
Items = { pivotItem1, pivotItem2 },
};

WindowHelper.WindowContent = SUT;
await WindowHelper.WaitForIdle();

SUT.SelectedIndex.Should().Be(0);
SUT.SelectedItem.Should().Be(pivotItem1);

SUT.SelectedItem = pivotItem2;

SUT.SelectedIndex.Should().Be(1);
SUT.SelectedItem.Should().Be(pivotItem2);
}

private class MyContext
{
public MyContext()
Expand Down
13 changes: 11 additions & 2 deletions src/Uno.UI/UI/Xaml/Controls/Pivot/Pivot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,17 @@ private static void OnTitlePropertyChanged(DependencyObject dependencyObject, De

private void OnSelectedItemPropertyChanged(object oldValue, object newValue)
{
var removedItems = oldValue == null ? new object[0] : new[] { oldValue };
var addedItems = newValue == null ? new object[0] : new[] { newValue };
var removedItems = oldValue == null ? Array.Empty<object>() : new[] { oldValue };
var addedItems = newValue == null ? Array.Empty<object>() : new[] { newValue };

if (newValue is PivotItem newItem && newItem.PivotHeaderItem is PivotHeaderItem headerItem)
{
var newIndex = _staticHeader.Children.IndexOf(headerItem);
if (newIndex > -1)
{
SelectedIndex = newIndex;
}
}

OnSelectedItemChangedPartial(oldValue, newValue);

Expand Down

0 comments on commit 166e7a7

Please sign in to comment.