diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue6387.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue6387.cs new file mode 100644 index 000000000000..9f0028e9b813 --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue6387.cs @@ -0,0 +1,23 @@ +namespace Controls.TestCases.HostApp.Issues; + +[Issue(IssueTracker.Github, 6387, "ArgumentException thrown when a negative value is set for the padding of a label", PlatformAffected.UWP)] +public class Issue6387 : ContentPage +{ + public Issue6387() + { + Label labelWithNegativePaddingValue = new Label + { + AutomationId = "LabelWithNegativePaddingValue", + Text = "The test passes if the app runs without crashing and fails if the app crashes", + Padding = new Thickness(-2) + }; + + VerticalStackLayout verticalStackLayout = new VerticalStackLayout + { + Padding = 20, + Children = { labelWithNegativePaddingValue } + }; + + Content = verticalStackLayout; + } +} diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue6387.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue6387.cs new file mode 100644 index 000000000000..0c58626ac433 --- /dev/null +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue6387.cs @@ -0,0 +1,21 @@ +using NUnit.Framework; +using UITest.Appium; +using UITest.Core; + +namespace Microsoft.Maui.TestCases.Tests.Issues; + +public class Issue6387 : _IssuesUITest +{ + public Issue6387(TestDevice device) : base(device) + { + } + + public override string Issue => "ArgumentException thrown when a negative value is set for the padding of a label"; + + [Test] + [Category(UITestCategories.Label)] + public void LabelWithNegativePaddingShouldNotThrowException() + { + App.WaitForElement("LabelWithNegativePaddingValue"); + } +} \ No newline at end of file diff --git a/src/Core/src/Platform/Windows/TextBlockExtensions.cs b/src/Core/src/Platform/Windows/TextBlockExtensions.cs index 52f953006e75..90de45765f18 100644 --- a/src/Core/src/Platform/Windows/TextBlockExtensions.cs +++ b/src/Core/src/Platform/Windows/TextBlockExtensions.cs @@ -5,6 +5,7 @@ using System.Xml.Linq; using System.Xml.Resolvers; using Microsoft.UI.Xaml.Controls; +using WThickness = Microsoft.UI.Xaml.Thickness; using Microsoft.UI.Xaml.Documents; namespace Microsoft.Maui.Platform @@ -31,8 +32,18 @@ public static void UpdateText(this TextBlock platformControl, ILabel label) public static void UpdateTextColor(this TextBlock platformControl, ITextStyle text) => platformControl.UpdateProperty(TextBlock.ForegroundProperty, text.TextColor); - public static void UpdatePadding(this TextBlock platformControl, ILabel label) => - platformControl.UpdateProperty(TextBlock.PaddingProperty, label.Padding.ToPlatform()); + public static void UpdatePadding(this TextBlock platformControl, ILabel label) + { + // Label padding values do not support negative values; if specified, negative values will be replaced with zero + var padding = new WThickness( + Math.Max(0, label.Padding.Left), + Math.Max(0, label.Padding.Top), + Math.Max(0, label.Padding.Right), + Math.Max(0, label.Padding.Bottom) + ); + + platformControl.UpdateProperty(TextBlock.PaddingProperty, padding); + } public static void UpdateCharacterSpacing(this TextBlock platformControl, ITextStyle label) {