From 166e7a783692e80cad48263fb4e5c5c77b05eaed Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Tue, 7 Sep 2021 10:21:46 +0200 Subject: [PATCH] fix: Changing Pivot.SelectedItem should reflect on Pivot.SelectedIndex --- .../Windows_UI_Xaml_Controls/Given_Pivot.cs | 23 +++++++++++++++++++ src/Uno.UI/UI/Xaml/Controls/Pivot/Pivot.cs | 13 +++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_Pivot.cs b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_Pivot.cs index e82836c2a9b9..e86c71e54fa4 100644 --- a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_Pivot.cs +++ b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_Pivot.cs @@ -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() diff --git a/src/Uno.UI/UI/Xaml/Controls/Pivot/Pivot.cs b/src/Uno.UI/UI/Xaml/Controls/Pivot/Pivot.cs index a033f5a33bf5..08d0756bf434 100644 --- a/src/Uno.UI/UI/Xaml/Controls/Pivot/Pivot.cs +++ b/src/Uno.UI/UI/Xaml/Controls/Pivot/Pivot.cs @@ -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() : new[] { oldValue }; + var addedItems = newValue == null ? Array.Empty() : 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);