在触摸坐标处获取UI元素

时间:2012-01-25 08:30:52

标签: c# wpf touch coordinates

我确信我已经读到有一种方法可以在WPF应用程序中获得TouchDown上的坐标,并找出这些触摸下面的UI元素。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

您必须针对可视树执行hit test

以下是鼠标单击的示例(但在这方面,触摸或多或少相同):

// 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.
    } 
}

HitTest的其他重载可以为您提供多个点击视觉效果。

答案 1 :(得分:0)

让你的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应该保存引发触摸事件的UI元素<{1}}

我希望这会有所帮助!