设置完在Canvas的位置后,控件HitTest不响应的问题
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)
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不响应的问题的更多相关文章
- WPF 获取鼠标屏幕位置、窗口位置、控件位置
		原文:WPF 获取鼠标屏幕位置.窗口位置.控件位置 public struct POINT { public int X; public int Y; public POINT(int x, int ... 
- vs设计界面出现“建控件时出错 响应在此上下文中不可用”
		使用VS2010设计Asp.net时出现: 只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态.还请确保在应用程序配置的 \\ 节中包括 ... 
- 结合ItemsControl在Canvas中动态添加控件的最MVVM的方式
		今天很开心的收获: ItemsControl 中 ItemsPanel的重定义和 ItemContainerStyle 以及 ItemTemplate 三者的巧妙结合,在后台代码不实例化任何控件的前提 ... 
- 设置VS2010中自带的ActiveX控件测试容器TstCon
		ActiveX控件:可以看做一个极小的服务器应用程序,他不能单独运行,需要嵌入到某个程序中才可以运行,我们可以自己写一个程序来测试自己写的程序(具体方法在下一篇文章阐述),第二种方法是利用VS(本人编 ... 
- c# asp.net 鼠标改变控件坐标位置,更改控件坐标,注册表保存读取,打印,查找局域网内打印机等等收集
		界面虽然被我弄的很难看,但功能还可以 里边注册表的路径自己设置一下,或者加一个创建注册表的语句,不然会报错 前台: <%@ Page Language="C#" AutoEv ... 
- jquery设置checkbox状态,设置dropdownlist选中值,隐藏某控件,给某控件追加东西
		jquery设置checkbox状态 $("[ID$=chkType]").attr("checked", true); jquery设置dropdownlis ... 
- dorado 的学习位置、控件使用方法查找、示例演示地址
		dorado的学习位置: http://wiki.bsdn.org/display/dorado7/Project+Home dorado的控件使用方法查找: http://dorado7.bsdn. ... 
- wpf中手风琴控件Accordion编辑模板后控件不正常。
		昨天有个网友Accordion控件从sl迁移到wpf时候显示不正常.也是就没有效果. 我也是sl做的比较多,wpf玩的少,Accordion模板里触发器,状态组调了一早上都没达到满意效果, 无奈只有百 ... 
- WPF触屏Touch事件在嵌套控件中的响应问题
		前几天遇到个touch事件的坑,记录下来以增强理解. 具体是 想把一个listview嵌套到另一个listview,这时候如果list view(子listview)的内容过多超过容器高度,它是不会出 ... 
随机推荐
- Unity3d LineRenderer画线
			原地址:http://www.cnblogs.com/88999660/archive/2013/01/21/2869498.html 1. 画多条线 画多条线需要在场景中放置多个GameObjec ... 
- MySQL建立索引的注意事项
			对于大数据量的表格,尤其是百万行以上的数据表,一定要对其建立索引,否则查询速度极慢.(参考后面的测试结果)建立索引时需注意: MySQL的索引有两种:单列索引(即在某一列上建索引).多列组合索引(即在 ... 
- spring boot实战(第十三篇)自动配置原理分析
			前言 spring Boot中引入了自动配置,让开发者利用起来更加的简便.快捷,本篇讲利用RabbitMQ的自动配置为例讲分析下Spring Boot中的自动配置原理. 在上一篇末尾讲述了Spring ... 
- MVC 详细说明
			.NET MVC执行过程: 1.网址路由比对 2.执行Controller与Action 3.执行View并返回结果 在使用MVC中是由IgnoreRoute()辅助方法对比成功的,会导致程序直接跳离 ... 
- 72  [面试题]如果不使用if-else和比较运算符,你知道如何求解2个数字中的较大一个吗?
			[本文链接] http://www.cnblogs.com/hellogiser/p/max-of-numbers-without-comparations.html [题目] 不使用if-else和 ... 
- php请求URL中的参数有空格
			url=http://www.123.com/abc.php?name=ku xiong ku xiong之间有一个空格,需要替换成%20或者+ url=http://www.123.com/abc. ... 
- Android涉及到的设计模式
			转载地址:http://blog.csdn.net/dengshengjin2234/article/details/8502097 1.适配器模式:ListView或GridView的Adapter ... 
- LinuxC语言读取文件,分割字符串,存入链表,放入另一个文件
			//file_op.c #include <string.h> #include <stdio.h> #include <stdlib.h> struct info ... 
- ListView滑动删除效果实现
			通过继承ListView然后结合PopupWindow实现 首先是布局文件: delete_btn.xml:这里只需要一个Button <?xml version="1.0" ... 
- OGNL表达式struts2标签“%,#,$”
			一.什么是OGNL,有什么特点? OGNL(Object-Graph Navigation Language),大概可以理解为:对象图形化导航语言.是一种可以方便地操作对象属性的开源表达式语言.OGN ... 
