Skip to content

Commit a47a875

Browse files
SyedAbdulAzeemSF4852PureWeen
authored andcommitted
[Windows] Fix for ArgumentException thrown when setting a negative padding value for the label (#29163)
* [Windows] Fix for 6387 ( Negative Padding values fail ). * [Windows] Fix for 6387 ( Negative Padding values fail ). * Have added Test case * Added a comment line explaining the assignment of zero when a padding value is negative * Have modified the test case
1 parent 9eae09c commit a47a875

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace Controls.TestCases.HostApp.Issues;
2+
3+
[Issue(IssueTracker.Github, 6387, "ArgumentException thrown when a negative value is set for the padding of a label", PlatformAffected.UWP)]
4+
public class Issue6387 : ContentPage
5+
{
6+
public Issue6387()
7+
{
8+
Label labelWithNegativePaddingValue = new Label
9+
{
10+
AutomationId = "LabelWithNegativePaddingValue",
11+
Text = "The test passes if the app runs without crashing and fails if the app crashes",
12+
Padding = new Thickness(-2)
13+
};
14+
15+
VerticalStackLayout verticalStackLayout = new VerticalStackLayout
16+
{
17+
Padding = 20,
18+
Children = { labelWithNegativePaddingValue }
19+
};
20+
21+
Content = verticalStackLayout;
22+
}
23+
}
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 Issue6387 : _IssuesUITest
8+
{
9+
public Issue6387(TestDevice device) : base(device)
10+
{
11+
}
12+
13+
public override string Issue => "ArgumentException thrown when a negative value is set for the padding of a label";
14+
15+
[Test]
16+
[Category(UITestCategories.Label)]
17+
public void LabelWithNegativePaddingShouldNotThrowException()
18+
{
19+
App.WaitForElement("LabelWithNegativePaddingValue");
20+
}
21+
}

src/Core/src/Platform/Windows/TextBlockExtensions.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Xml.Linq;
66
using System.Xml.Resolvers;
77
using Microsoft.UI.Xaml.Controls;
8+
using WThickness = Microsoft.UI.Xaml.Thickness;
89
using Microsoft.UI.Xaml.Documents;
910

1011
namespace Microsoft.Maui.Platform
@@ -31,8 +32,18 @@ public static void UpdateText(this TextBlock platformControl, ILabel label)
3132
public static void UpdateTextColor(this TextBlock platformControl, ITextStyle text) =>
3233
platformControl.UpdateProperty(TextBlock.ForegroundProperty, text.TextColor);
3334

34-
public static void UpdatePadding(this TextBlock platformControl, ILabel label) =>
35-
platformControl.UpdateProperty(TextBlock.PaddingProperty, label.Padding.ToPlatform());
35+
public static void UpdatePadding(this TextBlock platformControl, ILabel label)
36+
{
37+
// Label padding values do not support negative values; if specified, negative values will be replaced with zero
38+
var padding = new WThickness(
39+
Math.Max(0, label.Padding.Left),
40+
Math.Max(0, label.Padding.Top),
41+
Math.Max(0, label.Padding.Right),
42+
Math.Max(0, label.Padding.Bottom)
43+
);
44+
45+
platformControl.UpdateProperty(TextBlock.PaddingProperty, padding);
46+
}
3647

3748
public static void UpdateCharacterSpacing(this TextBlock platformControl, ITextStyle label)
3849
{

0 commit comments

Comments
 (0)