0

添付プロパティを使用して wpf の DragAndDrop-manager を実装しています。それは非常にうまく機能します。しかし、1 つだけ問題があります。ドラッグされたアイテムを取得するには、visualtree を使用しています。例として、リストボックスアイテムが必要ですが、元のソースはリストボックスアイテムの境界です。そのため、ヘルパー メソッドの 1 つを使用して、ListBoxItem 型の親を検索します。そのデータを取得してドラッグすることがわかった場合。

しかし、リストボックスを使用している間だけ、DragAndDrop-manager を実行可能にしたくありません。いいえ、すべての Itemscontrol で使用したいと思います。しかし、DataGrid は DataGridRows を使用し、リストビューは ListViewItem を使用します...コードを何度も何度も書かずに項目を取得する機会はありますか?

4

2 に答える 2

2

まあ、あなたはこの機能を持つことができます(私はそれを静的にすることを好みます):

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
    if (depObj != null)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
            if (child != null && child is T)
            {
                yield return (T)child;
            }

            foreach (T childOfChild in FindVisualChildren<T>(child))
            {
                yield return childOfChild;
            }
        }
    }
}

ある種のこれを使用します:
つまり、yourDependencyObjectToSearchIn コンテナー内のすべての TextBox 要素を検索したい場合

foreach (TextBox txtChild in FindVisualChildren<TextBox>(yourDependencyObjectToSearchIn))
{
    // do whatever you want with child of type you were looking for
    // for example:
    txtChild.IsReadOnly = true;
}

説明が欲しいなら、起きたらすぐにします)

于 2012-07-27T23:35:40.477 に答える
0

FrameworkElement または UIElement を使用してコントロールを識別できます。

継承階層を制御します。

System.Object

System.Windows.Threading.DispatcherObject

System.Windows.DependencyObject

  System.Windows.Media.Visual
    System.Windows.UIElement
      System.Windows.**FrameworkElement**
        System.Windows.Controls.Control
          System.Windows.Controls.ContentControl
            System.Windows.Controls.ListBoxItem
              System.Windows.Controls.**ListViewItem**

System.Object

System.Windows.Threading.DispatcherObject

System.Windows.DependencyObject

  System.Windows.Media.Visual

    System.Windows.UIElement
      System.Windows.**FrameworkElement**
        System.Windows.Controls.Control
          System.Windows.Controls.**DataGridRow**
于 2012-07-27T09:04:05.370 に答える