// private private void CreateAutoEditGrup() { if (ViewModel == null) { return; } ViewModel.ValidateFailed += ValidateFialed; //查找组 PropertyInfo[] properties = ViewModel.GetRealType().GetProperties().OfType <PropertyInfo>().Where(p => FilterProperty == null ? true : FilterProperty(p)).ToArray(); Dictionary <string, List <PropertyInfo> > groupDictionary = new Dictionary <string, List <PropertyInfo> >(); foreach (PropertyInfo p in properties) { AutoGenControlAttribute a = p.GetAttribute <AutoGenControlAttribute>(); if (a != null) { string groupName = a.GroupName ?? ""; if (groupDictionary.ContainsKey(groupName)) { List <PropertyInfo> list = groupDictionary[groupName]; list.Add(p); } else { groupDictionary.Add(groupName, new List <PropertyInfo> { p }); } } } foreach (var item in groupDictionary) { AutoEditGroup group = new AutoEditGroup(ViewModel, item.Key, FormPurpose, item.Value) { Dock = DockStyle.Top, Parent = this, }; group.SetHeaderVisible(ShowGroupHeader); group.BringToFront(); } }
//private private void CreateEditControl() { TableLayoutPanel layoutPanel = NewRow(); foreach (PropertyInfo p in _properties) { AutoGenControlAttribute a = p.GetAttribute <AutoGenControlAttribute>(); if (a == null) { continue; } //布局分为两块,左边和右边。 //如果左边被占用就放右边,右边也被占用就另起一行。 int colIndex = 0; //左 if (layoutPanel.GetControlFromPosition(colIndex, 0) != null) { //右 前提没有强制另起一行 if (a.BeginNewRow) { layoutPanel = NewRow(); } else { colIndex = 3; if (layoutPanel.GetControlFromPosition(colIndex, 0) != null) { //都占用就用新行的左边 layoutPanel = NewRow(); colIndex = 0; } } } //TODO: 长控件独占一行 // create Editor Control editor = (Control)Activator.CreateInstance(a.EditorType); editor.Name = a.EditorType.Name.LowerFirstLetter() + "_" + p.Name; editor.Dock = DockStyle.Fill; editor.Margin = new Padding(10, 12, 80, 10); editor.Enabled = a.Enabled; //read only var readOnlyProperty = (from pp in a.EditorType.GetProperties() where pp.Name == "ReadOnly" && pp.CanWrite select pp).FirstOrDefault(); if (readOnlyProperty != null) { switch (_formPurpose) { case EditFormPurpose.Create: readOnlyProperty.SetValue(editor, a.ReadOnlyWhenCreate, null); break; case EditFormPurpose.Modify: readOnlyProperty.SetValue(editor, a.ReadOnlyWhenModify, null); break; default: readOnlyProperty.SetValue(editor, true, null); break; } } editor.Parent = layoutPanel; layoutPanel.SetColumn(editor, colIndex + 1); layoutPanel.SetRow(editor, 0); //dataBind DefaultPropertyAttribute @default = editor.GetType().GetAttribute <DefaultPropertyAttribute>(); if (@default != null) { editor.DataBindings.Add(new Binding(@default.Name, _viewModel, p.Name, true, DataSourceUpdateMode.OnPropertyChanged)); } // create Label Label label = new Label { Name = "lable_" + p.Name, AutoSize = false, Dock = DockStyle.Fill, TextAlign = ContentAlignment.MiddleRight, Margin = new Padding(10, 11, 3, 11), Text = a.DisplayName ?? p.Name, Parent = layoutPanel, Tag = editor, }; layoutPanel.SetColumn(label, colIndex); layoutPanel.SetRow(label, 0); label.Click += Label_Click; } }