2009-09-03 1 views
0

사용자가 선택한 목록 상자에서 고무 밴드 또는 올가미 유형 선택을 허용하려고합니다. 내 목록 상자는 그리드에 있고 그리드에 내가 선택한 영역 위에 사각형을 그리는 컨트롤을 추가했습니다. 목록 상자 항목을 검사하여 사각형 내에 있는지 확인했지만 모두 반환하지는 않습니다. 해당 항목에 대해 VisualTreeHelper.GetDescendantBounds를 볼 때 (사각형을 X, Y로 가져 오는 것처럼) 항상 각 항목에 대해 X, Y를 0,0으로 반환합니다. 히트 테스트에 내가 뭘 잘못 했니?목록 상자 내에서 고무줄 유형 선택을 구현하는 WPF

답변

0

이 코드를 사용하면 다른 UIElement에 상대적인 UIElements의 위치와 범위를 가져올 수 있습니다. 코드는 this post에서 가져온 것입니다.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

public static class UIHelper 
{ 
    public static Boolean GetUIElementCornersRelativTo(UIElement Base, 
               UIElement RelativeTo, 
               ref Point TopLeft, 
               ref Point BottomLeft, 
               ref Point BottomRight, 
               ref Point TopRight, 
               ref String Message) 
    { 
     try 
     { 
      if (Base == null) 
      { 
       throw new Exception("Base UIElement is null"); 
      } 
      if (RelativeTo == null) 
      { 
       throw new Exception("RelativTo UIElement is null"); 
      } 

      TopLeft = Base.TranslatePoint(new Point(0, 0), RelativeTo); 
      BottomLeft = Base.TranslatePoint(new Point(0, Base.RenderSize.Height), RelativeTo); 
      BottomRight = Base.TranslatePoint(new Point(Base.RenderSize.Width, Base.RenderSize.Height), RelativeTo); 
      TopRight = Base.TranslatePoint(new Point(Base.RenderSize.Width, 0), RelativeTo); 

      Message = "OK"; 
      return true; 
     } 
     catch (Exception ex) 
     { 
      Message = ex.Message; 
      return false; 
     } 
    } 
    public static Boolean GetPointRelativTo(UIElement Base, 
            UIElement RelativeTo, 
            Point ToProjectPoint, 
            ref Point Result, 
            ref String Message) 
    { 
     try 
     { 
      if (Base == null) 
      { 
       throw new Exception("Base UIElement is null"); 
      } 
      if (RelativeTo == null) 
      { 
       throw new Exception("RelativTo UIElement is null"); 
      } 

      if (ToProjectPoint == null) 
      { 
       throw new Exception("To project point is null"); 
      } 

      Result = Base.TranslatePoint(ToProjectPoint, RelativeTo); 

      Message = "OK"; 
      return true; 
     } 
     catch (Exception ex) 
     { 
      Message = ex.Message; 
      return false; 
     } 
    } 
}