2012-11-02 31 views
0

我有一個容器控件正在處理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 
    } 

回答

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); 
      } 
     } 
    }