WPF Windows应用程序中 怎么遍历窗口上的所有控件,我在= = = = = = = = = = = = = = = = = = = = = =

鸣宇淳
博客专家认证
2009-07-24 12:24:39
WPF Windows应用程序中,我想遍历窗口上的所有控件,我想在WPF 浏览器应用程序中也差不多吧。恳求朋友们帮忙!
...全文
373 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ms44 2009-07-28
  • 打赏
  • 举报
回复
使用VisualTreeHelper的话, 代码如下:
定义扩展方法
public static IEnumerable<DependencyObject> GetVisuals(this DependencyObject root)
{
int count = VisualTreeHelper.GetChildrenCount(root);
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(root, i);
yield return child;
foreach (var descendants in child.GetVisuals())
{
yield return descendants;
}
}
}
调用如下(将所有Control禁掉):
LayoutRoot.GetVisuals().OfType<Control>().ToList().ForEach(item =>
{
item.IsEnabled = false;
});
ms44 2009-07-28
  • 打赏
  • 举报
回复
如果需要递归。就自己做个函数递归一下。
ms44 2009-07-28
  • 打赏
  • 举报
回复
foreach (UIElement element in LayoutRoot.Children)
{
if (element is UserControl)
{
UserControl current = ((UserControl)element);
}
}
Error_Code 2009-07-24
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20081007/19/60a21d46-0d3b-4f82-84a2-0e9185d86051.html
看这贴
鸣宇淳 2009-07-24
  • 打赏
  • 举报
回复
我想遍历的是窗口上“所有”的控件
ms44 2009-07-24
  • 打赏
  • 举报
回复
VisualTreeHelper

8,731

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧