简体   繁体   English

未通过UIElement时,Silverlight中的Canvas Click事件

[英]Canvas Click events in Silverlight when not over a UIElement

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. 我正在使用ScaleTransform对象缩放画布,以便可以放大/缩小。

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. 我已经连接好控件,以便可以拖动它们,并且通过使用MouseLeftButtonDown,MouseLeftButtonUp和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. 当我阅读canvas对象的文档时 ,我看到MouseLeftButtonDown事件仅在它通过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. 在鼠标指针悬停在UIElement上且按下鼠标左键(或笔尖接触Tablet PC)时发生。 (Inherited from UIElement.) (继承自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. 由于我是Silverlight的新手,所以我可能做错了方法。 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. 我正在寻找一点帮助,也许是很多方向。

我不是Silverlight专家,但是您可以在所有其他UIElement下方的Canvas添加一个透明UIElement ,并使用它来确定用户是否单击了其他任何可拖放元素之外。

You want to know when a click happens on the Canvas and isn't over other controls? 您想知道何时在Canvas上单击而不是在其他控件上单击吗?

The most natural thing is to capture the Canvas 's MouseLeftButtonDown . 最自然的是捕获CanvasMouseLeftButtonDown Inside of that event, take a peak to see where the click happened. 在该事件的内部,花一个高峰查看点击发生的位置。 Then peak at the UIElement s under the click. 然后在点击下的UIElement处达到峰值。 I recommend you keep everything in absolute coordinates to keep things straight. 我建议您将所有内容都保持绝对坐标以使内容保持笔直。 Something like: 就像是:

void Page_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    Point p = e.GetPosition(null);
    var elements =  VisualTreeHelper.FindElementsInHostCoordinates(p, App.Current.RootVisual);
    foreach (var element in elements)
    {
        //Figure out if you're over a particular UI element
    }

}

I think you may be interpreting the documentation wrong. 我认为您可能会误解文档。 According to MSDN, Canvas itself is an implementation of a UIElement : 根据MSDN,Canvas本身是UIElement的实现:

System.Windows.UIElement
  System.Windows.FrameworkElement
    System.Windows.Controls.Panel
      System.Windows.Controls.Canvas

From my experience, and correct me if I'm wrong, MouseLeftButtonDown usually only fires for the top-most UIElement clicked. 根据我的经验,如果我错了,请纠正我,MouseLeftButtonDown通常仅针对最顶部单击的UIElement触发。 So if you implement MouseLeftButtonDown for your Canvas, it should only fire when the Canvas is clicked, and NOT when the buttons are clicked. 因此,如果为Canvas实现MouseLeftButtonDown,则仅在单击Canvas时才触发,而在单击按钮时不触发。 I'd say try it out first. 我会说先尝试一下。

In WPF, I think this would be easily solved by routed events. 在WPF中,我认为这可以通过路由事件轻松解决。 However, Silverlight didn't get this feature. 但是,Silverlight没有获得此功能。 You may want to check out VisualTreeHelper.FindElementsInHostCoordinates . 您可能要签出VisualTreeHelper.FindElementsInHostCoordinates This article covers it a little bit. 本文涵盖了一点。

http://www.andybeaulieu.com/Default.aspx?tabid=67&EntryID=95 http://www.andybeaulieu.com/Default.aspx?tabid=67&EntryID=95

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Silverlight中删除UIElement的父级 - Remove parent of an UIElement in Silverlight 确定UIElement在画布上的位置 - Determine location of a UIElement on a Canvas 获取面板中的MousePosition(当鼠标悬停在另一个UIElement上时)到ViewModel - Get MousePosition in a Panel (when Mouse is over an other UIElement) to ViewModel Silverlight - 确定屏幕上是否显示UIElement - Silverlight - Determine if a UIElement is visible on screen 如何检查UIElement在Silverlight中是否可见? - How to check if a UIElement is visible in Silverlight? 与ObservableDictionary Silverlight的适当UIElement绑定 - Binding with an appropriate UIElement for ObservableDictionary Silverlight SIngle和双击事件无法在画布中按预期方式工作 - SIngle and Double click events not working as expected in canvas 带有形状的画布 - 当我点击形状时,MouseUp事件会同时触发它们 - Canvas with shape on it - MouseUp events fire on both of them when I click shape 以编程方式强制在Silverlight 3 UIElement(文本框)上进行验证 - Programmatically force validation on Silverlight 3 UIElement (Textbox) Silverlight:将UIElement绑定到ListBoxItems时发生TargetInvocationException - Silverlight: TargetInvocationException Occurred by Bind UIElement To ListBoxItems
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM