我试图允许Listbox
用户想要选择的项目中的橡皮筋或套索类型选择。我Listbox
在一个网格中,我在网格中添加了一个控件,该控件在我要选择的区域上绘制一个矩形。我已经尝试测试这些Listbox
项目以查看它们是否在矩形内,但它们似乎都返回它们没有。当查看VisualTreeHelper.GetDescendantBounds
这些项目时(就像我对矩形所做的那样,得到它的 X,Y)它总是将 X,Y 返回为每个项目的 0,0。我在命中测试方面做错了什么?
问问题
1502 次
1 回答
0
您可以使用此代码来获取 UIElements 相对于另一个 UIElement 的位置和边界。代码取自这篇文章。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
public static class UIHelper
{
public static Boolean GetUIElementCornersRelativTo(UIElement Base,
UIElement RelativeTo,
ref Point TopLeft,
ref Point BottomLeft,
ref Point BottomRight,
ref Point TopRight,
ref String Message)
{
try
{
if (Base == null)
{
throw new Exception("Base UIElement is null");
}
if (RelativeTo == null)
{
throw new Exception("RelativTo UIElement is null");
}
TopLeft = Base.TranslatePoint(new Point(0, 0), RelativeTo);
BottomLeft = Base.TranslatePoint(new Point(0, Base.RenderSize.Height), RelativeTo);
BottomRight = Base.TranslatePoint(new Point(Base.RenderSize.Width, Base.RenderSize.Height), RelativeTo);
TopRight = Base.TranslatePoint(new Point(Base.RenderSize.Width, 0), RelativeTo);
Message = "OK";
return true;
}
catch (Exception ex)
{
Message = ex.Message;
return false;
}
}
public static Boolean GetPointRelativTo(UIElement Base,
UIElement RelativeTo,
Point ToProjectPoint,
ref Point Result,
ref String Message)
{
try
{
if (Base == null)
{
throw new Exception("Base UIElement is null");
}
if (RelativeTo == null)
{
throw new Exception("RelativTo UIElement is null");
}
if (ToProjectPoint == null)
{
throw new Exception("To project point is null");
}
Result = Base.TranslatePoint(ToProjectPoint, RelativeTo);
Message = "OK";
return true;
}
catch (Exception ex)
{
Message = ex.Message;
return false;
}
}
}
于 2009-09-17T13:32:09.323 回答