设置完在Canvas的位置后,控件HitTest不响应的问题
2014-05-15 16:23
337 查看
have a Canvas with a couple of elements on it like Line, Path and Text Box. In the
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:
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
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
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:
or alternatively
MouseOverevent of the Canvas there's a
HitTestfor 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
HitTestfunction 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
pis given in coordinates relative to the
textBoxvisual. 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的位置
- 3013-12-11在OnSize中设置好控件的位置后,初始化的时候没有正常显示问题
- 如何设置和获得控件相对于Canvas的位置
- 关于android 中控件位置动态设置的问题
- c#创建窗体,向窗体添加控件,设置控件大小位置等属性,写控件的响应
- 关于最外层为相对布局时,通过动态设置布局内控件位置问题
- js设置控件的readonly和enabled属性问题
- FORM窗体 控件设置DOCK属性问题
- TextBox设置OnTextChanged后验证控件失效问题的解决
- DropDownList等列表类控件的SelectedValue设置出错的问题
- 问题:在网页中使用OCX,其EIDT控件不能响应backspace等键?
- DropDownList等列表类控件的SelectedValue设置出错的问题
- Canvas中的设置全屏的位置
- Flex中如何遍历TabBar控件的各个Tab,并且通过labelPlacement属性设置标签位置
- Office2007 Excel响应慢 搞了好久,好像是语言设置的问题!
- 服务端控件Enabled设置的问题
- WebBrowser控件——设置滚动条位置
- 程序编码问题..如何设置引用控件支持字符编码.解决传参中编码问题
- 设置RadioButton控件中文本的位置.