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. HTTP HSTS协议和 nginx

    导读 Netcraft 公司最近公布了他们检测SSL/TLS网站的研究,并指出只有仅仅5%的用户正确执行了HTTP严格传输安全HSTS.本文介绍nginx如何配置HSTS. 什么是HSTS HTTPS ...

  2. 即时聊天 / XMPP

    MQTT是第二个即时聊天协议(了解) 5.即时通讯 即时通讯网上有第三方的解决方案,比如环信,融云等.我们是自己搭的xmpp服务器,服务器使用的tigase,之前写过相关的博客,自己去年也做了对应的w ...

  3. 2015安徽省赛 C.LU的困惑

    题目描述 Master LU 非常喜欢数学,现在有个问题:在二维空间上一共有n个点,LU每连接两个点,就会确定一条直线,对应有一个斜率.现在LU把平面内所有点中任意两点连线,得到的斜率放入一个集合中( ...

  4. HDU 1231 最大连续子序列 &&HDU 1003Max Sum (区间dp问题)

    C - 最大连续子序列 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

  5. poj2996 模拟

    Help Me with the Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3713   Accepted:  ...

  6. vista/win7/win8区别

    1. Vista的内核版本号是:Windows 6.0:         Windows 7的内核是:Windows 6.1:         Windows 8的内核是:Windows 6.2   ...

  7. exFAT是支持Mac和Win的

    exFAT是支持Mac和Win的 转自: http://bbs.feng.com/read-htm-tid-8214017.html

  8. JTA集成JOTM或Atomikos配置分布式事务(Tomcat应用服务器)

    一.以下介绍Spring中直接集成JOTM提供JTA事务管理.将JOTM集成到Tomcat中. (经过测试JOTM在批量持久化时有BUG需要修改源码GenericPool类解决)! 参考文章http: ...

  9. oracle10G/11G官方下载地址集合 直接迅雷下载

    Oracle Database 11g Release 2 (11.2.0.1.0) for Microsoft Windows (64-bit)   http://download.oracle.c ...

  10. Java for LeetCode 068 Text Justification

    Given an array of words and a length L, format the text such that each line has exactly L characters ...