Skip to content

Commit d71a672

Browse files
[Windows] Fixed Margin doesn't work inside CollectionView EmptyView (#29897)
<!-- Please let the below note in for people that find this PR --> > [!NOTE] > Are you waiting for the changes in this PR to be merged? > It would be very helpful if you could [test the resulting artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from this PR and let us know in a comment if this change resolves your issue. Thank you! <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Issue Detail The Margin property is not applied as expected when used within the EmptyView of a CollectionView. Note: Issue not replicated in Android, iOS and Mac platforms in latest. ### Root Cause The Margin set on the EmptyView is not automatically applied to the native container (ContentControl) on the Windows platform, resulting in incorrect layout spacing. ### Description of Change Applied the EmptyView’s Margin explicitly to the _emptyView.Marging to ensure consistent layout rendering on Windows. ### Validated the behaviour in the following platforms - [x] Android - [x] Windows - [x] iOS - [x] Mac ### Issues Fixed: Fixes #8494 ### Screenshots | Before | After | |---------|--------| | <img src="https://github.com/user-attachments/assets/23f75266-60ec-4888-a177-bc484178c000"> | <img src="https://github.com/user-attachments/assets/6e9593df-f7fb-433c-994d-8462d6c528ac"> |
1 parent 37fc7eb commit d71a672

8 files changed

Lines changed: 93 additions & 0 deletions

File tree

src/Controls/src/Core/Handlers/Items/ItemsViewHandler.Windows.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,12 @@ protected virtual void UpdateEmptyView()
350350
break;
351351
}
352352

353+
if (_formsEmptyView is not null && _emptyView is not null)
354+
{
355+
var margin = _formsEmptyView.Margin;
356+
_emptyView.Margin = WinUIHelpers.CreateThickness(margin.Left, margin.Top, margin.Right, margin.Bottom);
357+
}
358+
353359
(ListViewBase as IEmptyView)?.SetEmptyView(_emptyView, _formsEmptyView);
354360

355361
UpdateEmptyViewVisibility();
28.7 KB
Loading
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System.Collections.ObjectModel;
2+
3+
namespace Maui.Controls.Sample.Issues;
4+
5+
[Issue(IssueTracker.Github, 8494, "Margin doesn't work inside CollectionView EmptyView", PlatformAffected.UWP)]
6+
7+
public class Issue8494 : ContentPage
8+
{
9+
Issue8494EmptyViewModel ViewModel;
10+
11+
public Issue8494()
12+
{
13+
ViewModel = new Issue8494EmptyViewModel();
14+
BindingContext = ViewModel;
15+
var label = new Label
16+
{
17+
Text = "EmptyView should be laid out with respect to the Specified margin",
18+
AutomationId = "EmptyViewDescriptionLabel",
19+
};
20+
21+
var emptyViewLayout = new StackLayout
22+
{
23+
Margin = new Thickness(40),
24+
Background = Colors.Yellow,
25+
Children =
26+
{
27+
new Label
28+
{
29+
Text = "EmptyView with Margin.",
30+
BackgroundColor = Colors.Blue,
31+
HorizontalTextAlignment = TextAlignment.Center,
32+
},
33+
}
34+
};
35+
36+
var collectionView = new CollectionView
37+
{
38+
ItemsSource = ViewModel.ItemList,
39+
BackgroundColor = Colors.Green,
40+
EmptyView = emptyViewLayout,
41+
};
42+
43+
var grid = new Grid
44+
{
45+
RowDefinitions =
46+
{
47+
new RowDefinition { Height = GridLength.Auto },
48+
new RowDefinition { Height = GridLength.Star },
49+
}
50+
};
51+
52+
grid.Add(label, 0, 0);
53+
grid.Add(collectionView, 0, 1);
54+
Content = grid;
55+
}
56+
}
57+
58+
public class Issue8494EmptyViewModel
59+
{
60+
public ObservableCollection<string> ItemList { get; set; }
61+
62+
public Issue8494EmptyViewModel()
63+
{
64+
ItemList = new ObservableCollection<string>();
65+
}
66+
}
16.9 KB
Loading
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues;
6+
7+
public class Issue8494 : _IssuesUITest
8+
{
9+
public Issue8494(TestDevice testDevice) : base(testDevice)
10+
{
11+
}
12+
public override string Issue => "Margin doesn't work inside CollectionView EmptyView";
13+
14+
[Test]
15+
[Category(UITestCategories.CollectionView)]
16+
public void CheckEmptyViewMargin()
17+
{
18+
App.WaitForElement("EmptyViewDescriptionLabel");
19+
VerifyScreenshot();
20+
}
21+
}
9.02 KB
Loading
35.2 KB
Loading
34.5 KB
Loading

0 commit comments

Comments
 (0)