在页面上,我将UserControl动态添加到GridView中。因此,每个UserControl可以包含不同类型的控件(TextBox,CheckBox,单选按钮)
例如,UserControl的名称是:UserForm。
问题:如何使用VisualTreeHelper获取控件集合并检查textBox是否为空。
我找到了与该问题类似的代码,并对其进行了修改,但无法正常工作。
我不知道这意味着什么,是否需要?
list.AddRange(AllTextBoxes(child))
我应该使用MyList.Select()还是MyList.Where()吗?
无效的FindTextBoxes() { 列表<TextBox> MyList = AllTextBoxes(UserForm); var count = MyList.Where(x => if(string.IsEmptyOrNull(x.Text)); } 列出<TextBox> AllTextBoxes(DependencyObject父对象) { var list = new List <TextBox>(); 为(int i = 0; i <VisualTreeHelper.GetChildrenCount(parent); i ++) { var child = VisualTreeHelper.GetChild(parent,i); 如果(子级为TextBox) list.Add(子级为TextBox); list.AddRange(AllTextBoxes(child)); } 返回清单; }
这是我用的。
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
var textBoxes = AllChildren(MyGridView).Where(x => x is TextBox);
}
public IEnumerable<Control> AllChildren(DependencyObject parent)
{
for (int index = 0; index < VisualTreeHelper.GetChildrenCount(parent); index++)
{
var child = VisualTreeHelper.GetChild(parent, index);
if (child is Control)
yield return child as Control;
foreach (var item in AllChildren(child))
yield return item;
}
}
祝你好运!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句