FindName in Silverlight --- Walk through the viual tree
2010-05-24 14:42
549 查看
How do you obtain a reference to an ancestor (within the visual tree) of a Silverlight FrameworkElement, given its name? The question seems pretty straight forward. FrameworkElement exposes a method, aptly named FindName, which appears to do just that. Easy solution, right? Well....maybe.
Using the FindName method works great, as long as the object you're looking for resides within the same XAML namescope as the FrameworkElement. How do you know if your target object is in the same XAML namescope as your FrameworkElement?
After my research, i found if the control is inside the usercontrols which you are referencing, these kind of controls will not be found by FindName method. While if the target object you are looking for is in the same level of the XAML, they can be found.
However, they are still part of the visual tree, so there should be some way to walk the tree down. FrameworkElement doesn't appear to expose any members to facilitate this type of action. Still, there must be a way to do it
I found a method somewhere for this solution, It will find the object regardless of the XAML namescope.
Using the FindName method works great, as long as the object you're looking for resides within the same XAML namescope as the FrameworkElement. How do you know if your target object is in the same XAML namescope as your FrameworkElement?
After my research, i found if the control is inside the usercontrols which you are referencing, these kind of controls will not be found by FindName method. While if the target object you are looking for is in the same level of the XAML, they can be found.
However, they are still part of the visual tree, so there should be some way to walk the tree down. FrameworkElement doesn't appear to expose any members to facilitate this type of action. Still, there must be a way to do it
I found a method somewhere for this solution, It will find the object regardless of the XAML namescope.
public static class VisualTreeWalker { public static FrameworkElement FindName(string name, DependencyObject reference) { return FindName<FrameworkElement>(name, reference); } public static T FindName<T>(string name, DependencyObject reference) where T : FrameworkElement { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException("name"); } if (reference == null) { throw new ArgumentNullException("reference"); } return FindNameInternal<T>(name, reference); } private static T FindNameInternal<T>(string name, DependencyObject reference) where T : FrameworkElement { foreach (DependencyObject obj in GetChildren(reference)) { T elem = obj as T; if (elem != null && elem.Name == name) { return elem; } elem = FindNameInternal<T>(name, obj); if (elem != null) { return elem; } } return null; } private static IEnumerable<DependencyObject> GetChildren(DependencyObject reference) { int childCount = VisualTreeHelper.GetChildrenCount(reference); for (int i = 0; i < childCount; i++) { yield return VisualTreeHelper.GetChild(reference, i); } } }
相关文章推荐
- (Struts2文件下载)Can not find a java.io.InputStream with the name [inputStream] in the invocation stack
- Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the <p
- Troubleshooting High I/O Wait in Linux --- A walkthrough on how to find processes that are causing h
- struts2文件下载出现Can not find a java.io.InputStream with the name [imageStream] in the invocation stack.
- Can not find a java.io.InputStream with the name [targetFile] in the invocation stack.
- Struts2 下载 Can not find a java.io.InputStream with the name [downloadFile] in the invocation stack
- Can not find a java.io.InputStream with the name [inputStream] in the invocation
- struts2下载文件时严重: Can not find a java.io.InputStream with the name [targetFile] in the invocation
- Can not find a java.io.InputStream with the name [downloadFile] in the invocation stack问题解决
- Programmatically Find an Ancestor in the Visual Tree
- Can not find a java.io.InputStream with the name [...] in the invocation stack. ...
- SSH 文件下载发生异常: Can not find a java.io.InputStream with the name [inputStream] in the invocation sta
- 数据库Error:The ScriptCollection in ScriptName not find
- Can not find a java.io.InputStream with the name [inputStream] in the invocation stack
- fwnx- find the common ancestor in a tree
- Can not find a java.io.InputStream with the name [inputStream] in the invocation stack
- ch4.6: find the LCA of 2 nodes in a binary tree with 3 different methods
- Struts2中下载项目路径以外的文件报错 Can not find a Java.io.InputStream with the name [downloadFile] in the invoc
- Can not find a java.io.InputStream with the name [] in the invocation stack 解决办法
- Find the second largest element in a Binary Search Tree