public static DependencyObject GetParent(DependencyObject obj)
{
if (obj == null)
return null;
ContentElement ce = obj as ContentElement;
if (ce != null)
{
DependencyObject parent = ContentOperations.GetParent(ce);
if (parent != null)
return parent;
FrameworkContentElement fce = ce as FrameworkContentElement;
return fce != null ? fce.Parent : null;
}
return VisualTreeHelper.GetParent(obj);
}
//ham chính để lay DependencyObject parent với kiểu đối tương là T
public static T FindAncestorOrSelf <T>(DependencyObject obj)
where T : DependencyObject
{
while (obj != null)
{
T objTest = obj as T;
if (objTest != null)
return objTest;
obj = GetParent(obj);
}
return null;
}
TrackBack URL for this entry:
http://code.logos.com/blog/2008/02/finding_ancestor_elements_in_w.html
No comments:
Post a Comment