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 all commits
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,8 @@ public FormsGridView()
DefaultStyleKey = typeof(FormsGridView);

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

ChoosingItemContainer += OnChoosingItemContainer;
}

public int Span
Expand Down Expand Up @@ -95,6 +96,11 @@ void FindItemsWrapGrid()
UpdateItemSize();
}

void OnChoosingItemContainer(ListViewBase sender, ChoosingItemContainerEventArgs args)
{
FindItemsWrapGrid();
}

void WrapGridSizeChanged(object sender, SizeChangedEventArgs e)
{
UpdateItemSize();
Expand All @@ -120,11 +126,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 +153,7 @@ 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));

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.