WPF - 命中测试过滤器回调
我有一个画布,它的 VisualCollection 中有子 DrawingVisuals。我想对某种类型的孩子进行命中测试,但不想对其他类型的孩子进行命中测试。为此,我编写了 HitTestFilterCallback 函数:
public HitTestFilterBehavior MyHitTestFilter(DependencyObject o)
{
Debug.WriteLine(o.GetType());
if (o is BackgroundLine)
{
return HitTestFilterBehavior.ContinueSkipSelf;
}
else
{
return HitTestFilterBehavior.Continue;
}
}
因此,我检查 canvas 的子元素是否是从 DrawingVisual 派生的 BackgroundLine,如果是,则跳过它。但是,我从 Debug.WriteLine(o.GetType()) 获得的类型只是 System.Windows.Media.DrawingVisual。有没有办法找到最具体的对象类型?
其余代码如下。我只想针对 GraphicsBase 对象进行测试。
GraphicsBase hit = null;
public HitTestResultBehavior MyHitTestResult(HitTestResult result)
{
hit = (GraphicsBase)result.VisualHit;
return HitTestResultBehavior.Stop;
}
VisualTreeHelper.HitTest(drawingCanvas, new HitTestFilterCallback(MyHitTestFilter),
new HitTestResultCallback(MyHitTestResult), new PointHitTestParameters(point));
if (hit != null)
Debug.WriteLine("hit");
else
Debug.WriteLine("nothing");
I have a canvas and it has child DrawingVisuals in its VisualCollection. I want to hit test against some type of child but not for others. To do that I wrote HitTestFilterCallback function:
public HitTestFilterBehavior MyHitTestFilter(DependencyObject o)
{
Debug.WriteLine(o.GetType());
if (o is BackgroundLine)
{
return HitTestFilterBehavior.ContinueSkipSelf;
}
else
{
return HitTestFilterBehavior.Continue;
}
}
So I check whether the child of canvas is a BackgroundLine, which is derived from DrawingVisual, and if it is I skip it. However, the type I am getting from Debug.WriteLine(o.GetType()) is only System.Windows.Media.DrawingVisual. Is there a way I can find the most specific object type?
Rest of the code is below. I want to test against GraphicsBase objects only.
GraphicsBase hit = null;
public HitTestResultBehavior MyHitTestResult(HitTestResult result)
{
hit = (GraphicsBase)result.VisualHit;
return HitTestResultBehavior.Stop;
}
VisualTreeHelper.HitTest(drawingCanvas, new HitTestFilterCallback(MyHitTestFilter),
new HitTestResultCallback(MyHitTestResult), new PointHitTestParameters(point));
if (hit != null)
Debug.WriteLine("hit");
else
Debug.WriteLine("nothing");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了问题。我看到的 DrawingVisual 对象是我为背景颜色添加的矩形。我忘记了这一点,并认为我正在将 BackgroundLine 对象的类型设置为 DrawingVisual。正如 rooks 所说,我可以获得特定的 BackgroundLine 类型。谢谢。
I found the problem. The DrawingVisual object I am seeing was the rectangle I added for background color. I forgot about that and thought I was getting BackgroundLine object's type as DrawingVisual. I can get the specific BackgroundLine type as rooks said. Thanks.