检测哪个子控件收到了Pointer *事件

时间:2012-11-02 01:42:24

标签: xaml windows-runtime

我有一个处理PointerPressed和PointerMoved事件的容器控件。

容器包含一组按钮。

在处理事件时,我该如何确定实际收到的按钮?

    mainPage.AddHandler(PointerPressedEvent, new PointerEventHandler(pointerPressedHandler), true);        
    private void pointerPressedHandler(object sender, PointerRoutedEventArgs e)
    {
        var p = e.GetCurrentPoint(null); // maybe can be done using position info?
        var s = e.OriginalSource as Border; // OriginalSource is a Border, not the Button, and I don't seem to be able to get to the Button from the Border

        // todo - determine which button was clicked
    }

1 个答案:

答案 0 :(得分:0)

这有效:

    private void pointerPressedHandler(object sender, PointerRoutedEventArgs e)
    {
        var p = e.GetCurrentPoint(null);
        var elements = VisualTreeHelper.FindElementsInHostCoordinates(p.Position, mainPage, false);

        Button foundButton;
        foreach (var item in elements)
        {
            foundButton = item as Button;
            if (foundButton != null)
            {
                System.Diagnostics.Debug.WriteLine("found button: " + foundButton.Name);
            }
        }
    }