File tree Expand file tree Collapse file tree 2 files changed +26
-3
lines changed
src/MahApps.Metro/Controls Expand file tree Collapse file tree 2 files changed +26
-3
lines changed Original file line number Diff line number Diff line change @@ -144,7 +144,7 @@ private void CustomValidationPopup_Loaded(object? sender, RoutedEventArgs e)
144
144
this . scrollViewer . ScrollChanged -= this . ScrollViewer_ScrollChanged ;
145
145
}
146
146
147
- this . scrollViewer = adornedElement . TryFindParent < ScrollViewer > ( ) ;
147
+ this . scrollViewer = adornedElement . GetVisualAncestor < ScrollViewer > ( ) ;
148
148
if ( this . scrollViewer != null )
149
149
{
150
150
this . scrollViewer . ScrollChanged += this . ScrollViewer_ScrollChanged ;
Original file line number Diff line number Diff line change @@ -63,8 +63,7 @@ public static IEnumerable<DependencyObject> GetAncestors(this DependencyObject c
63
63
64
64
/// <summary>
65
65
/// Returns full visual ancestry, starting at the leaf.
66
- /// <para>If element is not of <see cref="Visual"/> or <see cref="Visual3D"/> the
67
- /// logical ancestry is used.</para>
66
+ /// <para>If element is not of <see cref="Visual"/> or <see cref="Visual3D"/> the logical ancestry is used.</para>
68
67
/// </summary>
69
68
/// <param name="leaf">The starting object.</param>
70
69
/// <returns></returns>
@@ -79,6 +78,30 @@ public static IEnumerable<DependencyObject> GetVisualAncestry(this DependencyObj
79
78
}
80
79
}
81
80
81
+ /// <summary>
82
+ /// Tries to find and returns a visual ancestor, starting at the leaf.
83
+ /// <para>If element is not of <see cref="Visual"/> or <see cref="Visual3D"/> the logical ancestry is used.</para>
84
+ /// </summary>
85
+ /// <param name="leaf">The starting object.</param>
86
+ /// <returns></returns>
87
+ public static T ? GetVisualAncestor < T > ( this DependencyObject ? leaf )
88
+ where T : DependencyObject
89
+ {
90
+ while ( leaf is not null )
91
+ {
92
+ if ( leaf is T ancestor )
93
+ {
94
+ return ancestor ;
95
+ }
96
+
97
+ leaf = leaf is Visual or Visual3D
98
+ ? VisualTreeHelper . GetParent ( leaf )
99
+ : LogicalTreeHelper . GetParent ( leaf ) ;
100
+ }
101
+
102
+ return default ( T ) ;
103
+ }
104
+
82
105
/// <summary>
83
106
/// Finds a Child of a given item in the visual tree.
84
107
/// </summary>
You can’t perform that action at this time.
0 commit comments