have a Canvas with a couple of elements on it like Line, Path and Text Box. In the MouseOver event of the Canvas there's a HitTest for all of them like:

bool HitTest( Point p )
{
return VisualTreeHelper.HitTest( textBox, p ) != null;
}

This works pixel-accurate for Line and Path, but does not work as expected for Text Box.

For example when the TextBox is positioned at 50, 50 like this:

Canvas.SetLeft( textBox, 50.0 );
Canvas.SetTop( textBox, 50.0 );

it is drawn at that position correctly: if the text is about 20 pixels high and 50 pixels wide, the bounding rectangle on the screen is roughly L=50 T=50 R=100 B=70.

However the HitTest function returns false in that rectangle and returns true only in a rectangle L=0 T=0 R=50 B=20. In other words the hit test knows the size of the Text Box but ignores it is not positioned at 0,0.

Why does this happen, and how to work around? (I have the feeling it has something to do with the fact that for positioning the Line and Path elements I do not use SetLeft/SetTop)

 

 

 

up vote2down voteaccepted

Simply because the TextBox doesn't know anything about the fact that it is positioned in a Canvas, and hence nothing about an "actual position".

When you call VisualTreeHelper.HitTest(textBox, p) the Point p is given in coordinates relative to the textBox visual. Is doesn't matter where in a Canvas or any other possible container the TextBox is located.

That you get the "right" coordinates (relative to your Canvas) for Line and Path is just because you don't set Canvas.Left or Canvas.Top on them.

In case you need the coordinates relative to the Canvas, you would usually do something like this:

bool HitTest(UIElement element, Point p)
{
var elementPoint = new Point(
p.X - Canvas.GetLeft(element),
p.Y - Canvas.GetTop(element)); return VisualTreeHelper.HitTest(element, elementPoint) != null;
}

or alternatively

bool HitTest(UIElement element, Point p)
{
var elementPoint = canvas.TransformToDescendant(element).Transform(p); return VisualTreeHelper.HitTest(element, elementPoint) != null;

设置完在Canvas的位置后,控件HitTest不响应的问题的更多相关文章

  1. WPF 获取鼠标屏幕位置、窗口位置、控件位置

    原文:WPF 获取鼠标屏幕位置.窗口位置.控件位置 public struct POINT { public int X; public int Y; public POINT(int x, int ...

  2. vs设计界面出现“建控件时出错 响应在此上下文中不可用”

    使用VS2010设计Asp.net时出现: 只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态.还请确保在应用程序配置的 \\ 节中包括 ...

  3. 结合ItemsControl在Canvas中动态添加控件的最MVVM的方式

    今天很开心的收获: ItemsControl 中 ItemsPanel的重定义和 ItemContainerStyle 以及 ItemTemplate 三者的巧妙结合,在后台代码不实例化任何控件的前提 ...

  4. 设置VS2010中自带的ActiveX控件测试容器TstCon

    ActiveX控件:可以看做一个极小的服务器应用程序,他不能单独运行,需要嵌入到某个程序中才可以运行,我们可以自己写一个程序来测试自己写的程序(具体方法在下一篇文章阐述),第二种方法是利用VS(本人编 ...

  5. c# asp.net 鼠标改变控件坐标位置,更改控件坐标,注册表保存读取,打印,查找局域网内打印机等等收集

    界面虽然被我弄的很难看,但功能还可以 里边注册表的路径自己设置一下,或者加一个创建注册表的语句,不然会报错 前台: <%@ Page Language="C#" AutoEv ...

  6. jquery设置checkbox状态,设置dropdownlist选中值,隐藏某控件,给某控件追加东西

    jquery设置checkbox状态 $("[ID$=chkType]").attr("checked", true); jquery设置dropdownlis ...

  7. dorado 的学习位置、控件使用方法查找、示例演示地址

    dorado的学习位置: http://wiki.bsdn.org/display/dorado7/Project+Home dorado的控件使用方法查找: http://dorado7.bsdn. ...

  8. wpf中手风琴控件Accordion编辑模板后控件不正常。

    昨天有个网友Accordion控件从sl迁移到wpf时候显示不正常.也是就没有效果. 我也是sl做的比较多,wpf玩的少,Accordion模板里触发器,状态组调了一早上都没达到满意效果, 无奈只有百 ...

  9. WPF触屏Touch事件在嵌套控件中的响应问题

    前几天遇到个touch事件的坑,记录下来以增强理解. 具体是 想把一个listview嵌套到另一个listview,这时候如果list view(子listview)的内容过多超过容器高度,它是不会出 ...

随机推荐

  1. [Effective JavaScript笔记]第1条:了解使用的js版本

    1997年 正式成为国际标准,官方名称为ECMAScript. 1999年 定稿第3版ECMAScript标准(简称ES3),最广泛的js版本. 2009年 发布第5版即ES5,引入了一些新特性,标准 ...

  2. unity3d webplayer 16:9 居中显示模板

    原地址:http://www.cnblogs.com/88999660/archive/2013/04/12/3016773.html <!DOCTYPE html PUBLIC "- ...

  3. [ruby on rails] 跟我学之(8)修改数据

    修改views 修改index视图(app/views/posts/index.html.erb),添加编辑链接,如下: <h1>Our blogs</h1> <% @p ...

  4. 【转】Spring@Autowired注解与自动装配

    1   配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...

  5. 【转】MySQL Temporary Table相关问题的探究

    本文转载自:http://itindex.net/detail/10901-mysql-temporary-table 问题的引入 让我们先来观察几条非常简单的MySQL语句: mysql> c ...

  6. swift init继承问题

    当在子类的 designated init方法中不手动调用 父类的 designated init方法时,如果父类有不接受任何参数的init,那么系统会自动调用它,编译器不会报错.但是如果父类中没有不 ...

  7. DP:Bridging Signals(POJ 1631)

    不能交叉的引脚 (这一题的难度在于读题)题目大意:有一堆引脚(signals),左边一排,右边一排,左边从上到下,对应着连接右边的引脚(所有的引脚都被接上),现在引脚之间的连线有交叉,我们要桥接这些交 ...

  8. 连续自然数和(codevs 1312)

    题目描述 Description 对于一个自然数M,求出所有的连续的自然数段,使得这些连续自然数段的全部数字和为M.eg:1998+1999+2000+2001+2002=10000,所以从1998到 ...

  9. .pro配置选项

    在Qt Creator的项目中添加头文件和库   在Qt Creator中的工程中,工程通过.pro文件管理. 额外需要连接的连接库 unix:LIBS += -L your_lib_path -ly ...

  10. 现在不能使用foxmail同步qq记事本功能,可能是对字数的大小有限制

    那么在经过了两个星期的时间完成的主要功能就是幻灯片和站点管理,在之后还有更多的任务要做,本来从时间的安排上不太合理,在这个月底要基本完成,主要是其他的组员也有比较重的任务,那么就是需要随时有一个状态, ...