#region 获取控件
public TreeViewItem GetParentObjectEx<TreeViewItem>(DependencyObject obj) where TreeViewItem : FrameworkElement
{
DependencyObject parent = VisualTreeHelper.GetParent(obj);
while (parent != null)
{
if (parent is TreeViewItem)
{
return (TreeViewItem)parent;
}
parent = VisualTreeHelper.GetParent(parent);
}
return null;
}
private childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is childItem)
return (childItem)child;
else
{
childItem childOfChild = FindVisualChild<childItem>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
public ucTreeViewItemHeader GetTreeViewItemByPoint( Point point) {
UIElement element = this.InputHitTest(point) as UIElement;
return GetParentElement(element);
}
public ucTreeViewItemHeader GetParentElement(UIElement element)
{
try
{
if (element == null) { return null; }
ucTreeViewItemHeader treeViewItem = null;
if (element is ucTreeViewItemHeader)
{
treeViewItem = element as ucTreeViewItemHeader;
}
else
{
element = VisualTreeHelper.GetParent(element) as UIElement;
treeViewItem = GetParentElement(element);
}
return treeViewItem;
}
catch (Exception ex)
{
MessageBox.Show("GetParentElement" + ex.Message);
throw ex;
}
}
#endregion
|