2010-01-13 62 views

回答

3

最接近的等效值爲VisualTreeHelper.HitTest。它的工作方式與Silverlight的FindElementsInHostCoordinates有很大不同,但您應該可以使用它來滿足您的需求。

3

假如你有在Silverlight這樣的呼叫

var result = VisualTreeHelper.FindElementsInHostCoordinates(myPoint, myUIElement); 

那麼這個WPF代碼應當具有同等result

var result = new List<DependencyObject>(); 
         //changed from external edits, because VisualHit is 
         //only a DependencyObject and may not be a UIElement 
         //this could cause exceptions or may not be compiling at all 
         //simply filter the result for class UIElement and 
         //cast it to IEnumerable<UIElement> if you need 
         //the very exact same result including type 

VisualTreeHelper.HitTest(
    myUiElement, 
    null, 
    new HitTestResultCallback(
     (HitTestResult hit)=>{ 
      result.Add(hit.VisualHit); 
      return HitTestResultBehavior.Continue; 
     }), 
    new PointHitTestParameters(myPoint)); 

在你的特殊情況下,你可能想使用GeometryHitTestParameters而不是PointHitTestParameters到做一個Rect測試。