2011-08-12 7 views
3

名前が「audioPanel」のApp.xamlグリッドに含まれる要素を見つける方法はありますか? 私が試した:ビジュアルツリーの要素の検索方法は? wp7

Grid found = this.FindChild<Grid>(^*I can't find anything suitable*^, "audioPanel");

How can I find WPF controls by name or type?

UPD:App.xaml http://pastebin.com/KfWbjMV8

+0

なぜapp.xamlに 'Grid'がありますか? – AnthonyWJones

+0

私はすべてのページに表示されるオーディオパネルを持っています。 – SevenDays

答えて

2

更新:私の回答とH.B.の回答の両方の組み合わせが必要です。あなたは携帯電話のアプリケーションフレームをスタイリングしているので、HBさんのコメントから、「それが適用されているコントロールは、」かなりそうである下記FindChildのバージョンを使用し、

var grid = FindChild<Grid>(Application.Current.RootVisual, "audioPanel"); 

見えるようにFindChildにお電話を変更RootVisual(これには例外があるかもしれませんが、わかりません)。

また、私はpastebinのApp.xamlの "..."部分にContentPresenterがあると仮定しています。そうしないと、あなたのスタイルがうまくいかないと思います。

ENDのUPDATE

あなたは(WPF ways to find controls)にリンクされ、あなたの「audioPanel」グリッドは、別のグリッドの内部にネストされた問題の受け入れ答えを使用している場合、あなたはまだそれを見つけることができません - がありますそのコードに誤りがあります。ここではコントロールがネストされている場合でも動作します更新されたバージョンです:

public static T FindChild<T>(DependencyObject parent, string childName) 
     where T : DependencyObject 
    { 
     // Confirm parent and childName are valid. 
     if (parent == null) 
     { 
      return null; 
     } 

     T foundChild = null; 

     int childrenCount = VisualTreeHelper.GetChildrenCount(parent); 
     for (int i = 0; i < childrenCount; i++) 
     { 
      DependencyObject child = VisualTreeHelper.GetChild(parent, i); 
      // If the child is not of the request child type child 
      var childType = child as T; 
      if (childType == null) 
      { 
       // recursively drill down the tree 
       foundChild = FindChild<T>(child, childName); 

       // If the child is found, break so we do not overwrite the found child. 
       if (foundChild != null) 
       { 
        break; 
       } 
      } 
      else if (!string.IsNullOrEmpty(childName)) 
      { 
       var frameworkElement = child as FrameworkElement; 
       // If the child's name is set for search 
       if (frameworkElement != null && frameworkElement.Name == childName) 
       { 
        // if the child's name is of the request name 
        foundChild = (T) child; 
        break; 
       } 

       // Need this in case the element we want is nested 
       // in another element of the same type 
       foundChild = FindChild<T>(child, childName); 
      } 
      else 
      { 
       // child element found. 
       foundChild = (T) child; 
       break; 
      } 
     } 

     return foundChild; 
    } 
} 
+0

NullReferenceException、グリッドはnull.How私はこの問題を解決することができますか? – SevenDays

+0

これは私のためにも動作しません:(http://pastebin.com/tbFXRV5Nフルファイル。 – SevenDays

1

それはApp.xamlである場合、私は、あるリソースとして、それはApplication.Resources内のリソースの一部であることを前提とします視覚的なツリーにはどこにも使われていませんが、これはしません。

これが当てはまる場合は、リソースからオブジェクトのルートを取得し、そこから検索することができます。

var root = Application.Current.Resources["MyKey"] as FrameworkElement; 
Grid found = this.FindChild<Grid>(root, "audioPanel"); 
+0

「MyKey」とは何ですか?「mainFrameStyle」に変更すると、NullReferenceExceptionが発生します。 – SevenDays

+0

これはスタイルであるため、スタイル自体を取得する必要はなく、代わりに適用されるコントロールです。 ( '' MyKey''は '' mainFrameStyle''でした) –

+0

NullReferenceException。 http://pastebin.com/veb09ViS – SevenDays

0

がちょうど完全見つかりサブチャ​​イルズが上書きされるよう、E. Z. Hartのバージョンでは、バグがあります。ここには動作するバージョンがあります

public static T FindChild<T>(this DependencyObject parent, string childName = null) where T : DependencyObject 
    { 
     // Confirm parent and childName are valid. 
     if (parent == null) 
      return null; 

     T foundChild = null; 

     var childrenCount = VisualTreeHelper.GetChildrenCount(parent); 
     for (var i = 0; foundChild == null && i < childrenCount; i++) 
     { 
      var child = VisualTreeHelper.GetChild(parent, i);     

      // If the child is not of the request child type child 
      var childType = child as T; 
      if (childType == null) 
      {     
       // recursively drill down the tree 
       foundChild = FindChild<T>(child, childName);      
      } 
      else if (!string.IsNullOrEmpty(childName)) 
      { 
       var frameworkElement = child as FrameworkElement; 
       // If the child's name is set for search 
       if (frameworkElement != null && frameworkElement.Name == childName) 
       { 
        // if the child's name is of the request name 
        foundChild = (T)child;       
       } 
       else 
       { 
        // Need this in case the element we want is nested 
        // in another element of the same type 
        foundChild = FindChild<T>(child, childName); 
       }      
      } 
      else 
      { 
       // child element found. 
       foundChild = (T)child;      
      } 
     } 

     return foundChild; 
    } 
関連する問題