Silverlight 中不在 UIElement 上时的 Canvas Click 事件
我有一个画布对象,我在它上面洒满了奇妙的控件。 我正在使用 ScaleTransform 对象来缩放画布,以便我可以放大/缩小。
我已经连接了控件,以便可以拖动它们,并且通过使用 MouseLeftButtonDown、MouseLeftButtonUp 和 MouseMove 可以很好地进行拖放操作。 现在,我想在仅单击画布时启用事件。 当我阅读 文档 对于画布对象,我发现 MouseLeftButtonDown 事件仅在位于 UIElement 上时才会触发。
鼠标左键按下时发生 按下(或者当笔尖 触摸平板电脑),而鼠标 指针位于 UIElement 上方。 (继承自UIElement。)
不幸的是,我想要相反的行为。 我想知道何时在画布上单击鼠标而鼠标指针不在任何控件上。 由于我是 Silverlight 的新手,因此我可能会以错误的方式执行此操作。 有什么我忽略的吗? 我是否以错误的方式处理这个问题? 我正在寻求一些帮助,也许还有很多方向。
I have a canvas object and I am sprinkling fantastic controls all over it. I'm using a ScaleTransform object to scale the canvas so I can zoom in/out.
I have wired up the controls so that I can drag them around, and the drag and drop works well by using MouseLeftButtonDown, MouseLeftButtonUp, and MouseMove. Now, I want to work on enabling an event when I click only on the Canvas. When I read the docs for the canvas object, I see that the MouseLeftButtonDown event only fires when it's over a UIElement.
Occurs when the left mouse button is
pressed (or when the tip of the stylus
touches the tablet PC) while the mouse
pointer is over a UIElement.
(Inherited from UIElement.)
Unfortunately, I want the opposite behavior. I want to know when the mouse is clicked on the Canvas while the mouse pounter isn't over any controls. Since I'm new to Silverlight, I could be doing this the wrong way. Is there something I have overlooked? Am I going about this the wrong way? I'm looking for a little help, and perhaps a lot of direction.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不是 Silverlight 专家,但您能否将透明的
UIElement
添加到Canvas
的所有其他UIElement
下方,并使用它来确定如果用户单击了任何其他可拖/放元素的外部。I'm no Silverlight guru, but could you add a transparent
UIElement
to theCanvas
, below all otherUIElement
s, and use it to determine if the user has clicked outside of any of the other drag/drop-able elements.您想知道何时单击发生在
Canvas
上而不是其他控件上?最自然的事情是捕获
Canvas
的MouseLeftButtonDown
。 在该事件中,观察一下点击发生的位置。 然后在点击下的UIElement
处达到峰值。 我建议您将所有内容保持在绝对坐标中,以保持一切正常。 就像是:You want to know when a click happens on the
Canvas
and isn't over other controls?The most natural thing is to capture the
Canvas
'sMouseLeftButtonDown
. Inside of that event, take a peak to see where the click happened. Then peak at theUIElement
s under the click. I recommend you keep everything in absolute coordinates to keep things straight. Something like:我认为您可能错误地解释了文档。 根据 MSDN,Canvas 本身是
UIElement
的实现:根据我的经验,如果我错了,请纠正我,MouseLeftButtonDown 通常只会在单击最上面的 UIElement 时触发。 因此,如果您为 Canvas 实现 MouseLeftButtonDown,则它应该仅在单击 Canvas 时触发,而不是在单击按钮时触发。 我想说先尝试一下。
I think you may be interpreting the documentation wrong. According to MSDN, Canvas itself is an implementation of a
UIElement
:From my experience, and correct me if I'm wrong, MouseLeftButtonDown usually only fires for the top-most UIElement clicked. So if you implement MouseLeftButtonDown for your Canvas, it should only fire when the Canvas is clicked, and NOT when the buttons are clicked. I'd say try it out first.
在 WPF 中,我认为这可以通过路由事件轻松解决。 然而,Silverlight并没有获得这个功能。 您可能需要查看
VisualTreeHelper.FindElementsInHostCooperatives
。 本文对此进行了一些介绍。http://www.andybeaulieu.com/Default.aspx?tabid= 67&EntryID=95
In WPF, I think this would be easily solved by routed events. However, Silverlight didn't get this feature. You may want to check out
VisualTreeHelper.FindElementsInHostCoordinates
. This article covers it a little bit.http://www.andybeaulieu.com/Default.aspx?tabid=67&EntryID=95