Skip to content

Commit

Permalink
fix(ListView): [iOS] Ensure that the DataContext propagates properly …
Browse files Browse the repository at this point in the history
…to Header/Footer

(cherry picked from commit 85e8594)
  • Loading branch information
jeromelaban authored and mergify[bot] committed Jul 25, 2023
1 parent 42453be commit f0fa91d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3022,6 +3022,52 @@ public async Task When_Footer_DataContext()
Assert.AreEqual(header2.DataContext.ToString(), header2.Text);
}

[TestMethod]
[RunsOnUIThread]
public async Task When_HeaderTemplate_DataContext()
{
TextBlock header = null;

var SUT = new ListView()
{
ItemContainerStyle = BasicContainerStyle,
HeaderTemplate = new DataTemplate(() =>
{
var s = new StackPanel
{
Background = new SolidColorBrush(Colors.Red),
Children = {
(header = new TextBlock { Text = "empty" }),
}
};

header.SetBinding(TextBlock.TextProperty, new Binding { Path = new PropertyPath("MyText") });

return s;
})
};

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

var container = header.FindFirstParent<ContentControl>();

var source = new[] {
new ListViewItem(){ Content = "item 1" },
};

SUT.ItemsSource = source;
await WindowHelper.WaitForIdle();

Assert.IsNull(header.DataContext);

SUT.DataContext = new When_Header_DataContext_Model("test value");
await WindowHelper.WaitForIdle();

Assert.AreEqual(SUT.DataContext, header.DataContext);
Assert.AreEqual("test value", header.Text);
}

private async Task When_Items_Are_Equal_But_Different_References_Common(Selector sut)
{
var obj1 = new AlwaysEqualClass();
Expand Down
14 changes: 12 additions & 2 deletions src/Uno.UI/UI/Xaml/Controls/ListViewBase/ListViewBaseSource.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -401,14 +401,24 @@ private UICollectionReusableView GetBindableSupplementaryView(
.Binding("Content", "");
}

supplementaryView.Content.ContentTemplate = template;

if (elementKind == NativeListViewBase.ListViewFooterElementKindNS || elementKind == NativeListViewBase.ListViewHeaderElementKindNS)
{
supplementaryView.Content.SetParent(Owner.XamlParent);
supplementaryView.Content.Content = context;

if (context is not null)
{
supplementaryView.Content.Content = context;
}

// We need to reset the DataContext as it may have been forced to null as a local value
// during ItemsControl.CleanUpContainer
// See https://github.com/unoplatform/uno/blob/54041db0bd6d5049d8efab90b097eaca936bfca1/src/Uno.UI/UI/Xaml/Controls/ItemsControl/ItemsControl.cs#L1200
supplementaryView.Content.ClearValue(ContentControl.DataContextProperty);
}
else
{
supplementaryView.Content.ContentTemplate = template;
supplementaryView.Content.DataContext = context;
}

Expand Down

0 comments on commit f0fa91d

Please sign in to comment.