Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing crash when setting empty string for SettingsCard's Header and Description #312

Merged
merged 2 commits into from
Jan 9, 2024
Merged
Changes from 1 commit
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
31 changes: 23 additions & 8 deletions components/SettingsControls/src/SettingsCard/SettingsCard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Expand All @@ -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);
}
Expand All @@ -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;
}
}
Loading