WPF中怎样遍历窗体下的所有控件

yangyuanling 2010-05-16 06:01:59
WPF中怎样遍历窗体下的所有控件。。。。急求。。各位帮帮忙
...全文
1322 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangyuanling 2010-05-20
  • 打赏
  • 举报
回复
还想请问一下myVisual要传入的是什么东西
谢谢了
zyntl 2010-05-17
  • 打赏
  • 举报
回复
如果是遍历所有子代的话,需要用到VisualTreeHelper,代码如下:

static public List<Visual> EnumVisual(Visual myVisual)
{
List<Visual> list = new List<Visual>();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(myVisual); i++)
{
Visual childVisual = (Visual)VisualTreeHelper.GetChild(myVisual, i);
if (childVisual != null)
{
list.Add(childVisual);
EnumVisual(childVisual);
}
}
return list;
}
yangyuanling 2010-05-17
  • 打赏
  • 举报
回复
我要用递归方法来实现,,因为Grid下面还有好多控件
noway8881 2010-05-16
  • 打赏
  • 举报
回复
看看你Window最外层的是哪个容器就用哪个。
我姓区不姓区 2010-05-16
  • 打赏
  • 举报
回复
你的控件应该都放在一个Grid里面吧?把layoutRoot改为你的Grid的name试试
yangyuanling 2010-05-16
  • 打赏
  • 举报
回复
this不可以this.Children啊
noway8881 2010-05-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 yangyuanling 的回复:]

引用 3 楼 wuyq11 的回复:
foreach (UIElement element in LayoutRoot.Children)
{
if (element is UserControl)
{
UserControl current = ((UserControl)element);
}
}

layoutRoot 是什么??
[/Quote]

。。。。。。
把这个layoutRoot改成this.
xk1126 2010-05-16
  • 打赏
  • 举报
回复
不好意思看错了!~
sorry!~
yangyuanling 2010-05-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wuyq11 的回复:]
foreach (UIElement element in LayoutRoot.Children)
{
if (element is UserControl)
{
UserControl current = ((UserControl)element);
}
}
[/Quote]
layoutRoot 是什么??
noway8881 2010-05-16
  • 打赏
  • 举报
回复
楼上的代码即可。
如果是浏览可视树。可以参考如下代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using System.Diagnostics;
using System.Windows.Input;
using System.Collections.ObjectModel;

namespace Microsoft.Samples.KMoore.WPFSamples.VisualTreeViewer
{
public class VisualTreeItem
{
public VisualTreeItem(DependencyObject element)
{
_element = element;
}

public IEnumerable<VisualTreeItem> Children
{
get
{
if (_children == null)
{
_children = new List<VisualTreeItem>(VisualTreeHelper.GetChildrenCount(_element));
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(_element); i++)
{
_children.Add(new VisualTreeItem(VisualTreeHelper.GetChild(_element, i)));
}
}
return _children;
}
}

public string Name
{
get
{
FrameworkElement fe = _element as FrameworkElement;
if (fe != null && !String.IsNullOrEmpty(fe.Name))
{
return Type + ":" + fe.Name;
}
else
{
return Type;
}
}
}

public string Type
{
get
{
return _element.GetType().Name;
}
}

private DependencyObject _element;

private List<VisualTreeItem> _children;
}

}


调用
_vti = new VisualTreeItem(listBox);

treeView.ItemsSource = _vti.Children;

wuyq11 2010-05-16
  • 打赏
  • 举报
回复
foreach (UIElement element in LayoutRoot.Children)
{
if (element is UserControl)
{
UserControl current = ((UserControl)element);
}
}

yangyuanling 2010-05-16
  • 打赏
  • 举报
回复
在WPF中Control没有Controls这个属性。。。。 是在WPF中
xk1126 2010-05-16
  • 打赏
  • 举报
回复
foreach(Control var in this.Controls)
{
var.text = 你要遍历的值;
}

110,559

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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