WPF(c#)编写一个获取某个页面内的所有文本框下拉框等的值的函数

6 篇文章 0 订阅

  在写项目代码的时候难免需要获取到一个页面的所有数据,但是其文本框下拉框啥的又多,需要一个一个获取文本框里面的值,实在头疼,我看到我的一些同学一个一个的获取那些值都能把整个电脑屏幕的长度都给占了。想着,不如直接获取所有的那些信息的控件集合,然后一个一个遍历获取他们值就得了。

一个页面的xaml基本是这样:

可以看到里面的框框条条很多,具体思路就是给包住所有那些的控件的grid添加一个name,到时候直接获取这个name直接获取她的所有子集和就像这样:

UIElementCollection panel = this.GridContent.Children;//获取控件

如果将控件里的值要放到某一个model里,需要控件的name与model的属性一一对应。

然后再写一个遍历这些集合的函数方法:

        public static T AssignmentModels<T>(UIElementCollection panel)
        {
            T obj = Activator.CreateInstance<T>();
            foreach (var PropertieInfo in typeof(T).GetProperties())
            {
                foreach (var panelInfo in panel)
                {
                    if (panelInfo is TextBox)
                    {
                        TextBox textBoxPanel = panelInfo as TextBox;
                        if (textBoxPanel.Name == PropertieInfo.Name)
                        {
                            if (PropertieInfo.PropertyType.FullName.ToLower() == "system.int32")//判断model的属性的数据类型是否为数字
                                {
                                    PropertieInfo.SetValue(obj, Convert.ToInt32(textBoxPanel.Text.Trim()), null);
                                }
                                else
                                    PropertieInfo.SetValue(obj, textBoxPanel.Text.Trim(), null);
                        }
                    }
                    else if (panelInfo is ComboBox)
                    {
                        ComboBox comboPanel = panelInfo as ComboBox;

                        if (comboPanel.Name == PropertieInfo.Name)
                        {
                            var boxItem = comboPanel.SelectedValue;
                            if (PropertieInfo.PropertyType.FullName.ToLower() == "system.int32")
                                PropertieInfo.SetValue(obj, Convert.ToInt32(boxItem), null);
                            else
                            {
                                if (boxItem == null)
                                    PropertieInfo.SetValue(obj, "", null);
                                else
                                    PropertieInfo.SetValue(obj, boxItem.ToString(), null);
                            }
                        }
                    }
                    else if (panelInfo is DatePicker)
                    {
                        DatePicker datePanel = panelInfo as DatePicker;
                        if (datePanel.Name == PropertieInfo.Name)
                            PropertieInfo.SetValue(obj, datePanel.SelectedDate, null);
                    }
                    else if (panelInfo is CheckBox)
                    {
                        CheckBox CheckPanel = panelInfo as CheckBox;
                        if (CheckPanel.Name == PropertieInfo.Name)
                        {
                            if (PropertieInfo.PropertyType.FullName.ToLower() == "system.boolean")
                                PropertieInfo.SetValue(obj, CheckPanel.IsChecked, null);
                            else PropertieInfo.SetValue(obj, CheckPanel.IsChecked.ToString(), null);
                        }
                    }
                }
            }
            return obj;
        }

使用:

UIElementCollection panel = this.GridContent.Children;//获取控件
BsItemInfo bsItemInfo = CommonFunction.AssignmentModels<BsItemInfo>(panel);

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值