Hit Test behavior

泪湿孤枕 提交于 2019-12-06 06:50:30

The UserControl1 is invisible. Its contents are visible, but your UserControl1 instance itself does not have any visuals of its own. (And it never will. A user control's job is really just to contain other things.)

Hit testing only reports elements that make a direct contribution to the visual tree. And since hit testing considers each element in isolation, this means that elements that are acting purely as containers don't show up. (And a related fact is that hit testing only considers the pixels that actually got painted. So if you have a Border where you've set the BorderBrush and a non-zero BorderThickness but you have no Background, the hit test will only consider the border outline to be a hit - points inside of the border will not be considered to hit the border because it's not painting anything in its interior.

If you need to do hit testing of a "this thing, or anything inside this thing" style, then either either

  1. use mouse enter/leave events - those bubble, and so they will get raised even on invisible container elements
  2. use IsMouseOver or
  3. use the hit test function you're using, passing the user control as the first argument, and treat any hit as an indication that the hit test point is inside the user control

The third one is more complex, but if you need to hit test points other than the one currently under the mouse, you'd need to use it.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!