如何将 UserControl 中的所有文本框添加到 GridView [英] How to get all the textBoxes in the UserControl added into GridView

查看:26
本文介绍了如何将 UserControl 中的所有文本框添加到 GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在页面上,我动态地将 UserControl 添加到 GridView 中.因此,每个 UserControl 可以包含不同类型的控件(TextBox、CheckBox、Radio Button)

比如说,UserControl的名字是:UserForm.

问题:如何使用 VisualTreeHelper 获取控件集合并检查 textBox 是否为空.

我发现了一个与此问题类似的代码并修改了它但不起作用.

我不知道这意味着什么以及是否需要这样做?

list.AddRange(AllTextBoxes(child))

我应该使用 MyList.Select() 还是 MyList.Where() ?

<前>无效的 FindTextBoxes(){List MyList = AllTextBoxes(UserForm);var count = MyList.Where(x= > if(string.IsEmptyOrNull(x.Text));}列表 AllTextBoxes(DependencyObject parent){var list = new List ();for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++){var child = VisualTreeHelper.GetChild(parent, i);如果(孩子是TextBox)list.Add(child as TextBox);list.AddRange(AllTextBoxes(child));}退货清单;}

解决方案

这是我使用的.

void MainPage_Loaded(object sender, RoutedEventArgs e){var textBoxes = AllChildren(MyGridView).Where(x => x is TextBox);}公共 IEnumerable<Control>AllChildren(DependencyObject parent){for (int index = 0; index < VisualTreeHelper.GetChildrenCount(parent); index++){var child = VisualTreeHelper.GetChild(parent, index);如果(孩子是控制)yield return child 作为 Control;foreach (AllChildren(child) 中的 var 项目)收益率返回项;}}

祝你好运!

On the Page, I add UserControl into GridView dynamically. So, each UserControl can contain different kind of controls ( TextBox, CheckBox, Radio Button)

say , the name of UserControl is : UserForm.

problem : How to get a collection of control using VisualTreeHelper and check if textBox is empty.

I found a code similiar to this problem and modified it but not working.

I dont know what this means and if this is required?

list.AddRange(AllTextBoxes(child))

Should I use MyList.Select() or MyList.Where() ?


void FindTextBoxes()
{

   List <TextBox> MyList = AllTextBoxes(UserForm);

   var count = MyList.Where(x= > if(string.IsEmptyOrNull(x.Text));

}




List <TextBox> AllTextBoxes(DependencyObject parent)
{
    var list = new List <TextBox>();

    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
    {
        var child = VisualTreeHelper.GetChild(parent, i);

        if (child is TextBox)

            list.Add(child as TextBox);

        list.AddRange(AllTextBoxes(child));
    }
    return list;
}


解决方案

Here's what I use.

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;
    }
}

Best of luck!

这篇关于如何将 UserControl 中的所有文本框添加到 GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆