[英]Get UI element at the touch co-ordinates
I'm sure I've read that there is a way of getting co-ordinates on TouchDown in a WPF app and finding out what UI elements are 'underneath' this touch. 我确信我已经读过一种方法,可以在WPF应用程序中获取TouchDown上的坐标,并找出该触摸“下方”的UI元素。 Can anyone help? 有人可以帮忙吗?
You have to do a hit test against the visual tree. 您必须对视觉树进行一次命中测试 。
Here is an example with mouse click (but touch is more or less the same thing in this respect): 这是鼠标单击的示例(但在这方面触摸几乎是相同的):
// Respond to the left mouse button down event by initiating the hit test.
private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// Retrieve the coordinate of the mouse position.
Point pt = e.GetPosition((UIElement)sender);
// Perform the hit test against a given portion of the visual object tree.
HitTestResult result = VisualTreeHelper.HitTest(myCanvas, pt);
if (result != null)
{
// Perform action on hit visual object.
}
}
Other overload of HitTest can give you multiple hit visuals. HitTest的其他重载可以为您提供多个匹配的视觉效果。
Let your UI elements extend UIElement class like this: 让您的UI元素像这样扩展UIElement类:
class MyUIElement : UIElement
{
protected override void OnManipulationStarting(System.Windows.Input.ManipulationStartingEventArgs e)
{
base.OnManipulationStarting(e);
UIElement involvedUIElement = e.Source as UIElement;
// to cancel the touch manipulaiton:
e.Cancel();
}
}
involvedUIElement should hold the UI elements that raised the touch event, if you need to cancel the manipulation for specific elements you just need to call e.Cancel();
relatedUIElement应该包含引发触摸事件的UI元素,如果您需要取消对特定元素的操作,则只需调用e.Cancel();
I hope this will help! 我希望这个能帮上忙!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.