@@ -24,17 +24,17 @@ public static IEnumerable<DependencyObject> VisualDepthFirstTraversal(this Depen
24
24
}
25
25
}
26
26
27
- public static bool IsDescendant ( this DependencyObject parent , DependencyObject node )
27
+ public static bool IsAncestorOf ( this DependencyObject parent , DependencyObject node )
28
28
{
29
29
return node != null && parent . VisualDepthFirstTraversal ( ) . Contains ( node ) ;
30
30
}
31
31
32
32
/// <summary>
33
- /// Returns full visual ancestory , starting at the leaf.
33
+ /// Returns full visual ancestry , starting at the leaf.
34
34
/// </summary>
35
35
/// <param name="leaf"></param>
36
36
/// <returns></returns>
37
- public static IEnumerable < DependencyObject > GetVisualAncestory ( this DependencyObject leaf )
37
+ public static IEnumerable < DependencyObject > GetVisualAncestry ( this DependencyObject leaf )
38
38
{
39
39
while ( leaf != null )
40
40
{
@@ -43,7 +43,7 @@ public static IEnumerable<DependencyObject> GetVisualAncestory(this DependencyOb
43
43
}
44
44
}
45
45
46
- public static IEnumerable < DependencyObject > GetLogicalAncestory ( this DependencyObject leaf )
46
+ public static IEnumerable < DependencyObject > GetLogicalAncestry ( this DependencyObject leaf )
47
47
{
48
48
while ( leaf != null )
49
49
{
@@ -52,17 +52,18 @@ public static IEnumerable<DependencyObject> GetLogicalAncestory(this DependencyO
52
52
}
53
53
}
54
54
55
- public static bool HasAncestor ( this DependencyObject leaf , DependencyObject ancestor )
55
+ public static bool IsDescendantOf ( this DependencyObject leaf , DependencyObject ancestor )
56
56
{
57
- var visualAncestry = leaf . GetVisualAncestory ( ) . ToArray ( ) ;
58
- if ( visualAncestry . Contains ( ancestor ) )
57
+ DependencyObject parent = null ;
58
+ foreach ( var node in leaf . GetVisualAncestry ( ) )
59
59
{
60
- return true ;
60
+ if ( Equals ( node , ancestor ) )
61
+ return true ;
62
+
63
+ parent = node ;
61
64
}
62
65
63
- var lastVisualAncestor = visualAncestry . LastOrDefault ( ) ;
64
- var logicalAncestry = GetLogicalAncestory ( lastVisualAncestor ) ;
65
- return logicalAncestry . Contains ( ancestor ) ;
66
+ return parent != null && parent . GetLogicalAncestry ( ) . Contains ( ancestor ) ;
66
67
}
67
68
}
68
69
}
0 commit comments