Fetch the repository succeeded.
using System;
using System.Windows;
using System.Windows.Media;
namespace MaterialDesignThemes.Wpf
{
internal static class TreeHelper
{
public static double GetVisibleWidth(FrameworkElement element, UIElement parent)
{
if (element == null) throw new ArgumentNullException(nameof(element));
if (parent == null) throw new ArgumentNullException(nameof(parent));
var location = element.TransformToAncestor(parent).Transform(new Point(0, 0));
int width = (int) Math.Floor(element.ActualWidth);
var hitTest = parent.InputHitTest(new Point(location.X + width, location.Y));
if (IsAncestorTill(hitTest as FrameworkElement, element, parent))
{
return width;
}
//BinarySearch here
int end = (int) Math.Floor(element.ActualWidth);
int start = 0;
while (start < end)
{
width = (end + start)/2;
hitTest = parent.InputHitTest(new Point(location.X + width, location.Y));
if (IsAncestorTill(hitTest as FrameworkElement, element, parent))
{
//Speed tweak
hitTest = parent.InputHitTest(new Point(location.X + width + 1, location.Y));
if (IsAncestorTill(hitTest as FrameworkElement, element, parent))
{
start = width;
}
else
{
return width;
}
}
else
{
end = width;
}
}
//for (int width = (int) Math.Floor(element.ActualWidth); width >= 0; width--)
//{
// var hitTest = parent.InputHitTest(new Point(location.X + width, location.Y));
//
// if (hitTest == null) continue;
//
// if (IsAncestorTill(hitTest as FrameworkElement, element, parent))
// {
// return width;
// }
//}
return element.ActualWidth;
}
private static bool IsAncestorTill(FrameworkElement element, object ancestor, object container)
{
if (element == null) return false;
FrameworkElement parent = element;
do
{
if (ReferenceEquals(parent, ancestor)) return true;
if (ReferenceEquals(parent, container)) return false;
} while ((parent = (parent.Parent ?? VisualTreeHelper.GetParent(parent)) as FrameworkElement) != null);
return false;
}
public static Visual FindMainTreeVisual(Visual visual)
{
DependencyObject root = null;
DependencyObject dependencyObject = visual;
while (dependencyObject != null)
{
root = dependencyObject;
dependencyObject = VisualTreeHelper.GetParent(dependencyObject);
}
return root as Visual;
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。