diff --git a/components/SettingsControls/src/SettingsCard/SettingsCard.cs b/components/SettingsControls/src/SettingsCard/SettingsCard.cs index ba645331..cd691bb4 100644 --- a/components/SettingsControls/src/SettingsCard/SettingsCard.cs +++ b/components/SettingsControls/src/SettingsCard/SettingsCard.cs @@ -247,20 +247,20 @@ private void OnDescriptionChanged() { if (GetTemplateChild(DescriptionPresenter) is FrameworkElement descriptionPresenter) { - descriptionPresenter.Visibility = Description != null - ? Visibility.Visible - : Visibility.Collapsed; + descriptionPresenter.Visibility = IsNullOrEmptyString(Description) + ? Visibility.Collapsed + : Visibility.Visible; } - + } private void OnHeaderChanged() { if (GetTemplateChild(HeaderPresenter) is FrameworkElement headerPresenter) { - headerPresenter.Visibility = Header != null - ? Visibility.Visible - : Visibility.Collapsed; + headerPresenter.Visibility = IsNullOrEmptyString(Header) + ? Visibility.Collapsed + : Visibility.Visible; } } @@ -274,7 +274,7 @@ private void CheckVerticalSpacingState(VisualState s) { // On state change, checking if the Content should be wrapped (e.g. when the card is made smaller or the ContentAlignment is set to Vertical). If the Content and the Header or Description are not null, we add spacing between the Content and the Header/Description. - if (s != null && (s.Name == RightWrappedState || s.Name == RightWrappedNoIconState || s.Name == VerticalState) && (Content != null) && (Header != null || Description != null)) + if (s != null && (s.Name == RightWrappedState || s.Name == RightWrappedNoIconState || s.Name == VerticalState) && (Content != null) && (!IsNullOrEmptyString(Header) || !IsNullOrEmptyString(Description))) { VisualStateManager.GoToState(this, ContentSpacingState, true); } @@ -295,4 +295,19 @@ private void CheckVerticalSpacingState(VisualState s) return FocusManager.GetFocusedElement() as FrameworkElement; } } + + private static bool IsNullOrEmptyString(object obj) + { + if (obj == null) + { + return true; + } + + if (obj is string objString && objString == string.Empty) + { + return true; + } + + return false; + } } diff --git a/components/SettingsControls/tests/Test_SettingsCard.cs b/components/SettingsControls/tests/Test_SettingsCard.cs index f4928c53..3ba535b6 100644 --- a/components/SettingsControls/tests/Test_SettingsCard.cs +++ b/components/SettingsControls/tests/Test_SettingsCard.cs @@ -11,6 +11,20 @@ namespace SettingsControlsExperiment.Tests; [TestClass] public partial class SettingsCardTestClass : VisualUITestBase { + [UIThreadTestMethod] + public void EmptyNameTest(SettingsCard card) + { + // See https://github.com/CommunityToolkit/Windows/issues/310#issue-2066181868 + card.Name = string.Empty; + } + + [UIThreadTestMethod] + public void EmptyDescriptionTest(SettingsCard card) + { + // See https://github.com/CommunityToolkit/Windows/issues/310#issue-2066181868 + card.Description = string.Empty; + } + // If you don't need access to UI objects directly or async code, use this pattern. [TestMethod] public void SimpleSynchronousExampleTest()