2011-07-01 7 views
0

私のWPFプロジェクトでは、System.Windows.Controls.UserControlコントロールがあります。どのようにそのcontolの中のコントロールを見つけるか?WPF + Controls.UserControl。どのように内部のコントロールを見つけるか?

+0

具体的なコードを教えてください。 XAMLで行うのか、コードで行うのかによって、さまざまな解決方法があります。 – jjrdk

+0

私はそれをサーバー側のコードで実行したい – Tony

答えて

1

あなたの質問を正しく理解していれば、VisualTreeを使用してください。

は、MSDNを参照してください:あなたはおそらく、この拡張メソッドなどのビジュアルツリーを、散歩したいという場合にはhttp://msdn.microsoft.com/en-us/library/dd409789.aspx

1

が行われます。

internal static T FindVisualChild<T>(this DependencyObject parent) where T : DependencyObject 
{ 
    if (parent == null) 
    { 
     return null; 
    } 

    DependencyObject parentObject = parent; 
    int childCount = VisualTreeHelper.GetChildrenCount(parent); 
    for (int i = 0; i < childCount; i++) 
    { 
     DependencyObject childObject = VisualTreeHelper.GetChild(parentObject, i); 
     if (childObject == null) 
     { 
      continue; 
     } 

     var child = childObject as T; 
     return child ?? FindVisualChild<T>(childObject); 
    } 

    return null; 
} 

それはあなたがコントロールあなたのタイプを知っている必要があります探しています。

関連する問題