diff --git a/src/MahApps.Metro/Controls/CustomValidationPopup.cs b/src/MahApps.Metro/Controls/CustomValidationPopup.cs index de6faecfeb..0e2ed44bae 100644 --- a/src/MahApps.Metro/Controls/CustomValidationPopup.cs +++ b/src/MahApps.Metro/Controls/CustomValidationPopup.cs @@ -144,7 +144,7 @@ private void CustomValidationPopup_Loaded(object? sender, RoutedEventArgs e) this.scrollViewer.ScrollChanged -= this.ScrollViewer_ScrollChanged; } - this.scrollViewer = adornedElement.TryFindParent(); + this.scrollViewer = adornedElement.GetVisualAncestor(); if (this.scrollViewer != null) { this.scrollViewer.ScrollChanged += this.ScrollViewer_ScrollChanged; diff --git a/src/MahApps.Metro/Controls/TreeHelper.cs b/src/MahApps.Metro/Controls/TreeHelper.cs index c1f8ee2052..cd108f228e 100644 --- a/src/MahApps.Metro/Controls/TreeHelper.cs +++ b/src/MahApps.Metro/Controls/TreeHelper.cs @@ -63,8 +63,7 @@ public static IEnumerable GetAncestors(this DependencyObject c /// /// Returns full visual ancestry, starting at the leaf. - /// If element is not of or the - /// logical ancestry is used. + /// If element is not of or the logical ancestry is used. /// /// The starting object. /// @@ -79,6 +78,30 @@ public static IEnumerable GetVisualAncestry(this DependencyObj } } + /// + /// Tries to find and returns a visual ancestor, starting at the leaf. + /// If element is not of or the logical ancestry is used. + /// + /// The starting object. + /// + public static T? GetVisualAncestor(this DependencyObject? leaf) + where T : DependencyObject + { + while (leaf is not null) + { + if (leaf is T ancestor) + { + return ancestor; + } + + leaf = leaf is Visual or Visual3D + ? VisualTreeHelper.GetParent(leaf) + : LogicalTreeHelper.GetParent(leaf); + } + + return default(T); + } + /// /// Finds a Child of a given item in the visual tree. ///