2009-09-03 23 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; 
     } 
    } 
} 
関連する問題