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
23 changes: 23 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue6387.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
15 changes: 13 additions & 2 deletions src/Core/src/Platform/Windows/TextBlockExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
{
Expand Down