Skip to content

Commit

Permalink
Fixing validation adorner for not yet visible controls
Browse files Browse the repository at this point in the history
  • Loading branch information
batzen committed Mar 22, 2023
1 parent 6f56d3f commit 8938e3e
Showing 1 changed file with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,6 @@ private static void OnValidationAdornerSiteForChanged(DependencyObject d, Depend
}
}


internal static void ShowValidationAdorner(DependencyObject targetElement, bool show)
{
// If the element has a VisualStateGroup for validation, then dont show the Adorner
Expand All @@ -363,7 +362,6 @@ internal static void ShowValidationAdorner(DependencyObject targetElement, bool
}
}


private static bool HasValidationGroup(FrameworkElement fe)
{
if (fe != null)
Expand Down Expand Up @@ -420,6 +418,26 @@ private static object ShowValidationAdornerOperation(object arg)
return null;
}

private static void ShowValidationAdornerWhenAdornerSiteGetsVisible(object sender, DependencyPropertyChangedEventArgs e)
{
var adornerSite = sender as UIElement;

if (adornerSite == null)
{
return;
}

adornerSite.IsVisibleChanged -= ShowValidationAdornerOnVisible;

DependencyObject targetElement = GetValidationAdornerSiteFor(adornerSite);
if (targetElement == null)
{
targetElement = adornerSite;
}

ShowValidationAdornerHelper(targetElement, adornerSite, (bool)e.NewValue && GetHasError(targetElement), false);
}

private static void ShowValidationAdornerHelper(DependencyObject targetElement, DependencyObject adornerSite, bool show, bool tryAgain)
{
UIElement siteUIElement = adornerSite as UIElement;
Expand All @@ -432,10 +450,19 @@ private static void ShowValidationAdornerHelper(DependencyObject targetElement,
{
if (tryAgain)
{
// try again later, perhaps giving layout a chance to create the adorner layer
adornerSite.Dispatcher.BeginInvoke(DispatcherPriority.Loaded,
new DispatcherOperationCallback(ShowValidationAdornerOperation),
new object[]{targetElement, adornerSite, show});
// Check if the element is visible, if not try to show the adorner again once it gets visible.
// This is needed because controls hosted in Expander or TabControl don't have a parent/AdornerLayer till the Expander is expanded or the TabItem is selected.
if (siteUIElement.IsVisible == false)
{
siteUIElement.IsVisibleChanged += ShowValidationAdornerWhenAdornerSiteGetsVisible;
}
else
{
// try again later, perhaps giving layout a chance to create the adorner layer
adornerSite.Dispatcher.BeginInvoke(DispatcherPriority.Loaded,
new DispatcherOperationCallback(ShowValidationAdornerOperation),
new object[]{targetElement, adornerSite, show});
}
}
return;
}
Expand Down

0 comments on commit 8938e3e

Please sign in to comment.