You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.3 KiB
71 lines
2.3 KiB
using System.Collections.Generic;
|
|
using System.Windows;
|
|
using System.Windows.Input;
|
|
using System.Windows.Media;
|
|
|
|
namespace SJ.Controls.Extensions
|
|
{
|
|
public static class VisualTreeExtension
|
|
{
|
|
public static T HitTest<T>(this FrameworkElement fe, Point? point) where T : FrameworkElement
|
|
{
|
|
if (point == null)
|
|
point = Mouse.GetPosition(fe);
|
|
var result = VisualTreeHelper.HitTest(fe, point.Value);
|
|
if (result == null)
|
|
return null;
|
|
if (result.VisualHit != null)
|
|
{
|
|
var r = FindParentOfType<T>(result.VisualHit);
|
|
return r;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查找父控件
|
|
/// </summary>
|
|
/// <typeparam name="T">父控件类型</typeparam>
|
|
/// <param name="obj">子控件实例</param>
|
|
/// <returns></returns>
|
|
public static T FindParentOfType<T>(this DependencyObject obj) where T : FrameworkElement
|
|
{
|
|
DependencyObject parent = VisualTreeHelper.GetParent(obj);
|
|
while (parent != null)
|
|
{
|
|
if (parent is T)
|
|
{
|
|
return (T)parent;
|
|
}
|
|
parent = VisualTreeHelper.GetParent(parent);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查找子控件
|
|
/// </summary>
|
|
/// <typeparam name="T">需要查找的控件类型</typeparam>
|
|
/// <param name="obj">父控件实例</param>
|
|
/// <returns></returns>
|
|
public static T FindFirstVisualChild<T>(this DependencyObject obj) where T : FrameworkElement
|
|
{
|
|
var queue = new Queue<DependencyObject>();
|
|
queue.Enqueue(obj);
|
|
while (queue.Count > 0)
|
|
{
|
|
DependencyObject current = queue.Dequeue();
|
|
for (int i = VisualTreeHelper.GetChildrenCount(current) - 1; 0 <= i; i--)
|
|
{
|
|
DependencyObject child = VisualTreeHelper.GetChild(current, i);
|
|
if (child != null && child is T)
|
|
{
|
|
return (T)child;
|
|
}
|
|
queue.Enqueue(child);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|