如何在可视树中找到元素? WP7

我怎么能找到包含在App.xaml中的元素,名为“audioPanel”的网格? 我试过了:

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

如何按名称或类型查找WPF控件?

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

更新:您需要我的答案和HB的答案的组合。 使用下面的FindChild版本,并将您对FindChild的调用更改为

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

由于您正在构建电话应用程序框架,因此HB评论中的“对其应用的控件”很可能是RootVisual(可能有例外,我不确定)。

另外,我假设你的app.xaml在pastebin中的“…”部分在那里有一个ContentPresenter,否则我认为你的风格不会起作用。

结束更新

如果您在链接的问题中使用了接受的答案( WPF查找控件的方法 ),并且’audioPanel’网格嵌套在另一个网格中,那么您仍然无法找到它 – 该代码中存在错误。 这是一个更新版本,即使控件嵌套也可以使用:

  public static T FindChild(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(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(child, childName); } else { // child element found. foundChild = (T) child; break; } } return foundChild; } } 

如果它在App.xaml我会认为它是Application.Resources中资源的一部分,因为任何地方都没有使用的资源不在可视化树中,这是不可行的。

如果这是真的,你可以尝试从资源中获取对象的根并从那里搜索,例如

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

为了完整起见 , EZ Hart的版本有一个bug,因为被发现的子子被覆盖了。 这是一个工作版本

 public static T FindChild(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(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(child, childName); } } else { // child element found. foundChild = (T)child; } } return foundChild; }