Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Windows] Fix wrong Layout from hidden CollectionView #17659

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue17400"
xmlns:ns="clr-namespace:Maui.Controls.Sample.Issues"
Title="Issue 17400">
<Grid
Padding="30,0"
VerticalOptions="Center"
RowDefinitions="*, Auto">
<CollectionView
x:Name="ItemsView"
ItemsSource="{Binding Items}"
IsVisible="{Binding AreItemsVisible}">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid
HeightRequest="60"
RowDefinitions=" 30,29,1">
<Label
AutomationId="WaitForStubControl"
Grid.Row="0"
Text="{Binding Label}"
FontSize="20"
FontAttributes="Bold"/>
<Label
Grid.Row="1"
Text="{Binding Description}"
FontSize="16"/>
<BoxView
Grid.Row="2"
HeightRequest="1"
Color="Red" />
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<Button
Grid.Row="1"
AutomationId="UpdateBtn"
Text="Change Visibility"
Command="{Binding UpdateCommand}"
HorizontalOptions="Center" />
</Grid>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;

namespace Maui.Controls.Sample.Issues
{
internal class Issue17400Item : BindableObject
{
public string Label { get; set; }

public string Description { get; set; }
}

internal class Issue17400ViewModel : BindableObject
{
ObservableCollection<Issue17400Item> _items;
bool _areItemsVisible;

public Issue17400ViewModel()
{
List<Issue17400Item> items = new List<Issue17400Item>();

foreach (var item in Enumerable.Range(0, 20))
{
items.Add(new Issue17400Item
{
Label = "Label " + item,
Description = "Description " + item,
});
}

Items = new ObservableCollection<Issue17400Item>(items);
AreItemsVisible = false;
}

public ObservableCollection<Issue17400Item> Items
{
get { return _items; }
set
{
_items = value;
OnPropertyChanged();
}
}

public bool AreItemsVisible
{
get { return _areItemsVisible; }
set
{
_areItemsVisible = value;
OnPropertyChanged();
}
}

public ICommand UpdateCommand => new Command(Update);

void Update()
{
AreItemsVisible = !AreItemsVisible;
}
}

[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, 17400, "CollectionView wrong Layout", PlatformAffected.UWP)]
public partial class Issue17400 : ContentPage
{
public Issue17400()
{
InitializeComponent();

BindingContext = new Issue17400ViewModel();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public FormsGridView()
DefaultStyleKey = typeof(FormsGridView);

RegisterPropertyChangedCallback(ItemsPanelProperty, ItemsPanelChanged);
Loaded += OnLoaded;
}

public int Span
Expand Down Expand Up @@ -120,11 +119,6 @@ void ItemsPanelChanged(DependencyObject sender, DependencyProperty dp)
FindItemsWrapGrid();
}

void OnLoaded(object sender, RoutedEventArgs e)
{
FindItemsWrapGrid();
}

public void SetEmptyView(FrameworkElement emptyView, View formsEmptyView)
{
_emptyView = emptyView;
Expand Down Expand Up @@ -152,10 +146,10 @@ protected override void OnApplyTemplate()

protected override global::Windows.Foundation.Size ArrangeOverride(global::Windows.Foundation.Size finalSize)
{
if (_formsEmptyView != null)
{
_formsEmptyView.Layout(new Rect(0, 0, finalSize.Width, finalSize.Height));
}
_formsEmptyView?.Layout(new Rect(0, 0, finalSize.Width, finalSize.Height));

if (_wrapGrid is null)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With an invisible CollectionView, the _wrapGrid will be null (VisualTreeHelper.GetChildrenCount will return 0) and will not apply the correct sizes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure to what extent this matters, but it looks like you can subscribe to ChoosingItemContainer and call FindItemsWrapGrid from there.

It just feels a bit weird to set the size information of the WrappedGrid inside the ArrangeOverride call.

Thoughts on just calling it from there?

public FormsGridView()
{
	// Using the full style for this control, because for some reason on 16299 we can't set the ControlTemplate
	// (it just fails silently saying it can't find the resource key)
	DefaultStyleKey = typeof(FormsGridView);

	RegisterPropertyChangedCallback(ItemsPanelProperty, ItemsPanelChanged);

	this.ChoosingItemContainer += FormsGridView_ChoosingItemContainer;
}

private void FormsGridView_ChoosingItemContainer(ListViewBase sender, ChoosingItemContainerEventArgs args)
{
	if (_wrapGrid is not null)
		FindItemsWrapGrid();
}

And then even for good measure

protected override global::Windows.Foundation.Size MeasureOverride(global::Windows.Foundation.Size availableSize)
{
	var result =  base.MeasureOverride(availableSize); // it looks like the grid comes into existence here.

	FindItemsWrapGrid();

	return result;
}

I also wonder if we should null at the grid and unsubscribe when the orientation is changed.

The Orientation applies a different template so the wrapgrid is going to change.
But maybe this GridView is recreated when the orientation gets changed?

		public Orientation Orientation
		{
			get => _orientation;
			set
			{
				_orientation = value;
				if (_orientation == Orientation.Horizontal)
				{
					ItemsPanel = (ItemsPanelTemplate)UWPApp.Current.Resources["HorizontalGridItemsPanel"];
					ScrollViewer.SetHorizontalScrollMode(this, WScrollMode.Auto);
					ScrollViewer.SetHorizontalScrollBarVisibility(this, UWPControls.ScrollBarVisibility.Auto);
				}
				else
				{
					ItemsPanel = (ItemsPanelTemplate)UWPApp.Current.Resources["VerticalGridItemsPanel"];
				}
			}
		}

FindItemsWrapGrid();

return base.ArrangeOverride(finalSize);
}
Expand Down
27 changes: 27 additions & 0 deletions src/Controls/tests/UITests/Tests/Issues/Issue17400.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.Maui.Appium;
using NUnit.Framework;

namespace Microsoft.Maui.AppiumTests.Issues
{
public class Issue17400 : _IssuesUITest
{
public Issue17400(TestDevice device)
: base(device)
{ }

public override string Issue => "CollectionView wrong Layout";

[Test]
public void Issue17400Test()
{
UITestContext.IgnoreIfPlatforms(new TestDevice[] { TestDevice.iOS, TestDevice.Android, TestDevice.Mac },
"Is a Windows issue; see https://github.com/dotnet/maui/issues/17400");

App.WaitForElement("UpdateBtn");
App.Tap("UpdateBtn");

App.WaitForElement("WaitForStubControl");
VerifyScreenshot();
jsuarezruiz marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.