我需要在WPF控件层次结构中搜索与给定名称或类型匹配的控件。我该怎么做呢?
当前回答
试试这个
<TextBlock x:Name="txtblock" FontSize="24" >Hai Welcom to this page
</TextBlock>
后面的代码
var txtblock = sender as Textblock;
txtblock.Foreground = "Red"
其他回答
试试这个
<TextBlock x:Name="txtblock" FontSize="24" >Hai Welcom to this page
</TextBlock>
后面的代码
var txtblock = sender as Textblock;
txtblock.Foreground = "Red"
这段代码只是修复了@CrimsonX回答的错误:
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++)
{
var child = VisualTreeHelper.GetChild(parent, i);
// If the child is not of the request child type child
T 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;
}
// 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
{
// child element found.
foundChild = (T)child;
break;
}
}
return foundChild;
}
你只需要继续递归调用方法,如果类型匹配,但名称不匹配(这发生在你传递FrameworkElement作为T时),否则它将返回null,这是错误的。
exciton80……我有一个问题,你的代码不递归通过用户控件。它击中了网格根并抛出了一个错误。我相信这能解决我的问题:
public static object[] FindControls(this FrameworkElement f, Type childType, int maxDepth)
{
return RecursiveFindControls(f, childType, 1, maxDepth);
}
private static object[] RecursiveFindControls(object o, Type childType, int depth, int maxDepth = 0)
{
List<object> list = new List<object>();
var attrs = o.GetType().GetCustomAttributes(typeof(ContentPropertyAttribute), true);
if (attrs != null && attrs.Length > 0)
{
string childrenProperty = (attrs[0] as ContentPropertyAttribute).Name;
if (String.Equals(childrenProperty, "Content") || String.Equals(childrenProperty, "Children"))
{
var collection = o.GetType().GetProperty(childrenProperty).GetValue(o, null);
if (collection is System.Windows.Controls.UIElementCollection) // snelson 6/6/11
{
foreach (var c in (IEnumerable)collection)
{
if (c.GetType().FullName == childType.FullName)
list.Add(c);
if (maxDepth == 0 || depth < maxDepth)
list.AddRange(RecursiveFindControls(
c, childType, depth + 1, maxDepth));
}
}
else if (collection != null && collection.GetType().BaseType.Name == "Panel") // snelson 6/6/11; added because was skipping control (e.g., System.Windows.Controls.Grid)
{
if (maxDepth == 0 || depth < maxDepth)
list.AddRange(RecursiveFindControls(
collection, childType, depth + 1, maxDepth));
}
}
}
return list.ToArray();
}
对代码的扩展。
增加了重载,以按类型、按类型和标准(谓词)查找一个子元素,查找符合标准的所有类型的子元素 FindChildren方法除了是DependencyObject的扩展方法外,还是一个迭代器 FindChildren还遍历逻辑子树。请参阅博客中链接的Josh Smith的文章。
来源: https://code.google.com/p/gishu-util/source/browse/#git%2FWPF%2FUtilities
解释性博客文章: http://madcoderspeak.blogspot.com/2010/04/wpf-find-child-control-of-specific-type.html
这些选项已经讨论了如何在c#中遍历可视化树。 使用RelativeSource标记扩展也可以遍历xaml中的可视树。msdn
按类型查找
Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type <TypeToFind>}}}"
推荐文章
- WPF数据绑定:我如何访问“父”数据上下文?
- 从System.Drawing.Bitmap中加载WPF BitmapImage
- 我应该使用什么MVVM框架?
- 在WPF中引入一个窗口到前面
- Visual Studio单击“查找结果”在错误的窗口中打开代码
- 窗口高度=“自动”未按预期工作
- 我如何得到一个动画gif在WPF工作?
- ListBox和ListView的区别是什么
- MVVM:从开始到结束的教程?
- 如何从查找“类型d”中排除此/ current / dot文件夹
- 按类型查找WPF窗口中的所有控件
- 项目控制与水平方向
- 用MVVM处理WPF中的对话框
- 显示所有集合中的所有内容
- 'find -exec'是Linux中的shell函数