Example #1
0
        protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
        {
            ControlDataItem controlDataItem = item as ControlDataItem;

            if (controlDataItem != null)
            {
                if (controlDataItem.Index % 3 == 0)
                {
                    return(BigItemTemplate);
                }
                else
                {
                    return(MediumItemTemplate);
                }
            }
            return(base.SelectTemplateCore(item, container));
        }
        }//页面显示的内容
        private void ModelToControlDataSource(bool isnew)
        {
            try
            {
                this.ControlDataSource.Clear();                                                   //先清空

                var model = this.SelectedItem == null ? this.OperationObject : this.SelectedItem; //无论是否有值,只要有类型即可
                foreach (var item in model.GetType().GetProperties())
                {
                    var list = item.GetCustomAttributes(typeof(UIIsEditInput), true);

                    if (list.Count() > 0)                            //需要用户输入的属性,才需要生成
                    {
                        ControlDataItem cdi = new ControlDataItem(); //一个绑定
                        //1、添加属性名称
                        cdi.PropertyName = item.Name;

                        //获取属性的UI显示名称
                        var    l1     = item.GetCustomAttributes(typeof(UIName), true);
                        UIName p1     = l1.Count() > 0 ? l1[0] as UIName : null;
                        string uiName = p1 == null ? item.Name : p1.Name;//如果UI显示名称是空,则按照属性名称显示

                        //2、设置显示名
                        cdi.DisplayName = uiName;

                        //3、显示必填提示
                        var          l1l2 = item.GetCustomAttributes(typeof(ValidNotNull), true);
                        ValidNotNull p1p2 = l1l2.Count() > 0 ? l1l2[0] as ValidNotNull : null;
                        Visibility   v    = p1p2 == null ? Visibility.Hidden : Visibility.Visible;//如果有必填要求,则显示必填提示

                        cdi.VisibleNotNull = v;

                        //3、设置类型
                        var l2 = item.GetCustomAttributes(typeof(UIEditControlType), true);
                        UIEditControlType p2 = l2.Count() > 0 ? l2[0] as UIEditControlType : null;
                        cdi.ControlType = p2 == null ? EnDic_EditControlType.文本框 : p2.ControlType;

                        //4、设置数据源
                        switch (cdi.ControlType)
                        {
                        case EnDic_EditControlType.拉框:
                            break;

                        case EnDic_EditControlType.单选框:
                            break;

                        case EnDic_EditControlType.框:
                            break;

                        case EnDic_EditControlType.字典下拉框:
                            cdi.Source = Customer.Utility.ComboboxUtility.GetEnDicCombobox(item);    //字典解析为集合
                            break;

                        case EnDic_EditControlType.文本框:
                            break;

                        case EnDic_EditControlType.时间控件:
                            break;

                        case EnDic_EditControlType.空:
                            break;

                        case EnDic_EditControlType.取文件:
                            break;

                        case EnDic_EditControlType.取路径:
                            break;

                        default:
                            break;
                        }
                        //5、根据是新增还是修改,来处理数据加载
                        if (!isnew)
                        {
                            //获取当前属性值
                            object proValue = item.GetValue(model);
                            if (proValue != null)
                            {
                                if ((cdi.ControlType == EnDic_EditControlType.拉框 || cdi.ControlType == EnDic_EditControlType.字典下拉框))
                                {
                                    if (cdi.Source != null)
                                    {
                                        var itemlists = ((ObservableCollection <dynamic>)cdi.Source);
                                        cdi.CurrentValue = itemlists.FirstOrDefault(p => p.Text == proValue.ToString());
                                    }
                                }
                                else
                                {
                                    cdi.CurrentValue = proValue;
                                }
                            }
                        }
                        //6、处理默认选中的情况(新增时,需要处理)
                        if (cdi.Source != null && cdi.CurrentValue == null)
                        {
                            cdi.CurrentValue = ((ObservableCollection <dynamic>)cdi.Source).FirstOrDefault();
                        }

                        //添加到数据解析集合
                        this.ControlDataSource.Add(cdi);
                    }
                }
            }
            catch (Exception)
            {
                this.ControlDataSource = new ObservableCollection <ControlDataItem>();
            }
        }