Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -450,12 +450,14 @@ protected virtual void UpdateItemsSource()
_itemFactory = new ItemFactory(Element);
PlatformView.ItemTemplate = _itemFactory;
}
else if (PlatformView.ItemTemplate is not null)
else if (PlatformView.ItemTemplate is ItemFactory)
{
// Preserve WinUI's default ItemContainer template when no MAUI template is set.
PlatformView.ItemTemplate = null;
}

PlatformView.ItemsSource = _collectionViewSource?.View;
// A CollectionViewSource with no Source does not provide a usable WinUI view.
PlatformView.ItemsSource = _collectionViewSource?.Source is null ? null : _collectionViewSource.View;
(PlatformView as MauiItemsView)?.InvalidateAutomationSetProperties();

if (PlatformView is MauiItemsView mauiItemsViewFlat)
Expand Down Expand Up @@ -534,11 +536,6 @@ void CleanUpCollectionViewSource()
}
}
}

if (VirtualView?.ItemsSource is null && PlatformView is not null)
{
PlatformView.ItemsSource = null;
}
}

void ItemsChanged(object? sender, NotifyCollectionChangedEventArgs e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,69 @@ namespace Microsoft.Maui.DeviceTests
{
public partial class CollectionViewTests
{
[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task CollectionView2ItemsSourceCanBeClearedAndRestored(bool useItemTemplate)
{
SetupBuilderCollectionView2();

var items = new List<string> { "Item 1", "Item 2", "Item 3" };
var collectionView = new CollectionView
{
ItemsSource = items,
EmptyView = new Label { Text = "No items" },
HeightRequest = 300,
WidthRequest = 300
};

if (useItemTemplate)
{
collectionView.ItemTemplate = new Controls.DataTemplate(() =>
{
var label = new Label();
label.SetBinding(Label.TextProperty, ".");
return label;
});
}

await CreateHandlerAndAddToWindow<CollectionViewHandler2>(collectionView, async handler =>
{
for (int i = 0; i < 2; i++)
{
handler.PlatformView.UpdateLayout();
Assert.Equal(items.Count,
Assert.IsAssignableFrom<System.Collections.IEnumerable>(handler.PlatformView.ItemsSource)
.Cast<object>().Count());
var platformItemTemplate = handler.PlatformView.ItemTemplate;
Assert.NotNull(platformItemTemplate);

collectionView.ItemsSource = null;
handler.PlatformView.UpdateLayout();
Assert.Null(handler.PlatformView.ItemsSource);
Assert.Contains((View)collectionView.EmptyView, collectionView.LogicalChildrenInternal);
if (!useItemTemplate)
{
Assert.Same(platformItemTemplate, handler.PlatformView.ItemTemplate);
}
await Task.Yield();

items = new List<string>(items);
collectionView.ItemsSource = items;
handler.PlatformView.UpdateLayout();
Assert.DoesNotContain((View)collectionView.EmptyView, collectionView.LogicalChildrenInternal);
if (!useItemTemplate)
{
Assert.Same(platformItemTemplate, handler.PlatformView.ItemTemplate);
}
}

Assert.Equal(items.Count,
Assert.IsAssignableFrom<System.Collections.IEnumerable>(handler.PlatformView.ItemsSource)
.Cast<object>().Count());
});
}

[Fact(DisplayName = "CollectionView Header Keeps Its BindingContext")]
public async Task CollectionViewHeaderKeepsItsBindingContext()
{
Expand Down
Loading