Skip to content

Commit

Permalink
fix: Correctly change PivotHeaderItem content when PivotItem.Header c…
Browse files Browse the repository at this point in the history
…hanges
  • Loading branch information
Youssef1313 committed Sep 7, 2021
1 parent 0789a59 commit 5a85199
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using FluentAssertions.Execution;
using static Private.Infrastructure.TestServices;
using Uno.UI.Extensions;
using Windows.UI.Xaml.Controls.Primitives;

namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls
{
Expand Down Expand Up @@ -71,6 +72,26 @@ public async Task Check_Binding()
items[1].Content.Should().Be(tbs2.ElementAt(0).Text);
}

[TestMethod]
[RunsOnUIThread]
public async Task Check_Changing_Header_Affects_UI()
{
var pivotItem = new PivotItem { Header = "Initial text" };
var SUT = new Pivot
{
Items = { pivotItem },
};

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

var pivotHeaderPanel = (PivotHeaderPanel)SUT.GetTemplateChild("StaticHeader");
var headerItem = (PivotHeaderItem)pivotHeaderPanel.Children.Single();
headerItem.Content.Should().Be("Initial text");
pivotItem.Header = "New text";
headerItem.Content.Should().Be("New text");
}

private class MyContext
{
public MyContext()
Expand Down
1 change: 1 addition & 0 deletions src/Uno.UI/UI/Xaml/Controls/Pivot/Pivot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ private void SynchronizeItems()

if (item is PivotItem pivotItem)
{
pivotItem.PivotHeaderItem = headerItem;
headerItem.Content = pivotItem.Header;
}
else
Expand Down
20 changes: 16 additions & 4 deletions src/Uno.UI/UI/Xaml/Controls/Pivot/PivotItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
using System.Drawing;
using System.Text;
using Uno.UI;
using Windows.UI.Xaml.Controls.Primitives;
#if XAMARIN_ANDROID
using Android.Views;
#endif

namespace Windows.UI.Xaml.Controls
{
public partial class PivotItem : ContentControl
{
public partial class PivotItem : ContentControl
{
public PivotItem()
{
this.HorizontalAlignment = HorizontalAlignment.Stretch;
Expand All @@ -27,6 +28,8 @@ public PivotItem(string header) : this()
Header = header;
}

internal PivotHeaderItem PivotHeaderItem { get; set; }

protected override bool CanCreateTemplateWithoutParent => true;

public object Header
Expand All @@ -35,8 +38,17 @@ public object Header
set { this.SetValue(HeaderProperty, value); }
}

public static DependencyProperty HeaderProperty { get ; } =
DependencyProperty.Register("Header", typeof(object), typeof(PivotItem), new FrameworkPropertyMetadata(null));
public static DependencyProperty HeaderProperty { get; } =
DependencyProperty.Register("Header", typeof(object), typeof(PivotItem), new FrameworkPropertyMetadata(null, propertyChangedCallback: OnHeaderChanged));

private static void OnHeaderChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
{
if (dependencyObject is PivotItem item && item.PivotHeaderItem != null)
{
item.PivotHeaderItem.Content = args.NewValue;

}
}

#if XAMARIN_ANDROID
// This allows the PivotItem to fill the whole available space.
Expand Down

0 comments on commit 5a85199

Please sign in to comment.