diff --git a/src/MahApps.Metro/Controls/CustomValidationPopup.cs b/src/MahApps.Metro/Controls/CustomValidationPopup.cs index a9c1e2dc8b..a0bf4bd529 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 e2709a2132..a1dd894fdb 100644 --- a/src/MahApps.Metro/Controls/TreeHelper.cs +++ b/src/MahApps.Metro/Controls/TreeHelper.cs @@ -58,6 +58,47 @@ 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. + /// + /// The starting object. + /// + public static IEnumerable GetVisualAncestry(this DependencyObject? leaf) + { + while (leaf != null) + { + yield return leaf; + leaf = leaf is Visual || leaf is Visual3D + ? VisualTreeHelper.GetParent(leaf) + : LogicalTreeHelper.GetParent(leaf); + } + } + + /// + /// 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 != null) + { + if (leaf is T ancestor) + { + return ancestor; + } + + leaf = leaf is Visual || leaf is Visual3D + ? VisualTreeHelper.GetParent(leaf) + : LogicalTreeHelper.GetParent(leaf); + } + + return default(T); + } + /// /// Finds a Child of a given item in the visual tree. ///