【NET】Winform用户控件的初步封装之编辑控件

编辑控件

  1 public abstract partial class TEditorBase <TEntity, TRepository, TSqlStrConstruct> : UserControl
  2         where TEntity:Yom.Extend.Entity.EntityBase
  3         where TRepository : Yom.Extend.Repository.RepositoryBaseRepository<TEntity, TSqlStrConstruct>
  4         where TSqlStrConstruct : Huawei.Data.SqlStrConstruct
  5     {
  6         protected TRepository repository = System.Activator.CreateInstance<TRepository>();
  7         public TEditorBase()
  8         {
  9             InitializeComponent();
 10             
 11             this.gpxTitle.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
 12                         | System.Windows.Forms.AnchorStyles.Left)
 13                         | System.Windows.Forms.AnchorStyles.Right)));
 14         }
 15         string key;
 16         public string Key
 17         {
 18             get {
 19                 return key;
 20             }
 21             set {
 22                 if (string.IsNullOrEmpty(value)) {
 23                     return;
 24                 }
 25                 key = value;
 26                 Entity = this.repository.FindBy(value) as Yom.Extend.Entity.EntityBase;
 27             }
 28         }
 29         protected abstract Action<EntityDataBindEventArgs> AfterMapDataToEntityAction
 30         {
 31             get;
 32         }
 33         protected abstract Action<EntityDataBindEventArgs> AfterMapDataToControlAction
 34         {
 35             get;
 36         }
 37         protected virtual Yom.Extend.Entity.EntityBase Entity
 38         {
 39             get {
 40                 TEntity entity = System.Activator.CreateInstance<TEntity>();
 41                 entity.PropertyValueSet(this.repository.GetEntityKeyName(), this.Key);
 42                 DataDetailGetFromControlChildren.MapDataToEntity(entity, this, AfterMapDataToEntityAction);
 43                 return entity;
 44             }
 45             set {
 46                 if (value == null)
 47                 {
 48                     return;
 49                 }
 50                 
 51                 DataDetailGetFromControlChildren.MapDataToControl(value, this, this.AfterMapDataToControlAction);
 52             }
 53         }
 54 
 55         protected virtual string Title
 56         {
 57             get {
 58                 return string.Empty;
 59             }
 60         }
 61         protected virtual bool IsValid(out string msg)
 62         {
 63             msg = string.Empty;
 64             return true;
 65         }
 66         protected virtual void SaveCallBack()
 67         {
 68             string msg = string.Empty;
 69             if (!IsValid(out msg))
 70             {
 71                 MessageBox.Show(msg);
 72                 return;
 73             }
 74             try
 75             {
 76                 if (string.IsNullOrEmpty(this.Key))
 77                 {
 78                     do
 79                     {
 80                         this.key = Guid.NewGuid().ToString();
 81                     } while (this.repository.Exists(this.key));
 82                     this.repository.Add(this.Entity);
 83                 }
 84                 else
 85                 {
 86                     this.repository.Update(this.Entity);
 87                 }
 88                 MessageBox.Show("保存成功!");
 89                 this.Hide();
 90                 this.Parent.Controls.Add(Lister);
 91                 this.Parent.Controls.Remove(this);
 92                 this.Dispose();
 93             }
 94             catch (Exception ex)
 95             {
 96                 MessageBox.Show(string.Format("保存失败:\r\n{0}", ex.Message));
 97             }
 98         }
 99 
100         protected virtual void CancelCallBack()
101         {
102             //this.Hide();
103             //this.Parent.Controls.Add(GetListControl());
104             //this.Parent.Controls.Remove(this);
105             //this.Dispose();
106             //this = null;
107             if (this.Parent.Controls.Count <= 1)
108             {
109                 this.Parent.Controls.Add(this.Lister);
110                 this.Parent.Controls.RemoveAt(0);
111             }
112             else
113             {
114                 this.Parent.Controls[0].Show();
115                 this.Parent.Controls.RemoveAt(1);
116             }
117         }
118 
119         private void bSave_Click(object sender, EventArgs e)
120         {
121             SaveCallBack();
122         }
123 
124         private void TEditorBase_Load(object sender, EventArgs e)
125         {
126             this.gpxTitle.Text = this.Title;
127         }
128         public abstract TListPager<TEntity, TRepository, TSqlStrConstruct> Lister
129         {
130             get;
131         }
132 
133         private void bCancel_Click(object sender, EventArgs e)
134         {
135             CancelCallBack();
136         }
137     }

属性赋值:

 1 public class EntityDataBindEventArgs : EventArgs
 2     {
 3 
 4         public Yom.Extend.Entity.EntityBase CurrentEntity
 5         {
 6             get;
 7             set;
 8         }
 9         public System.Reflection.PropertyInfo PropertyInfo
10         {
11             get;
12             set;
13         }
14         public System.Windows.Forms.Control BindControl
15         {
16             get;
17             set;
18         }
19     }
20     public class DataDetailGetFromControlChildren
21     {
22         public static void MapDataToEntity(Yom.Extend.Entity.EntityBase entity, System.Windows.Forms.Control parent, Action<EntityDataBindEventArgs> afterDataGet)
23         {
24 
25             System.Reflection.PropertyInfo[] pis = entity.GetType().GetProperties();
26             if (pis != null && pis.Length > 0) {
27                 System.Windows.Forms.Control c;
28                 foreach (System.Reflection.PropertyInfo pi in pis)
29                 {
30                     c = null;
31                     try
32                     {
33                         c = parent.Controls.Find(pi.Name, true)[0];
34                     }
35                     catch {
36                         continue;
37                     }
38                     if (c != null)
39                     {
40                         entity.PropertyValueSet(pi.Name, c.Text);
41                         if (afterDataGet != null)
42                         {
43                             afterDataGet(new EntityDataBindEventArgs() { CurrentEntity = entity, PropertyInfo = pi, BindControl = c });
44                         }
45                     }
46                 }
47             }
48         }
49         public static void MapDataToControl(Yom.Extend.Entity.EntityBase entity, System.Windows.Forms.Control parent, Action<EntityDataBindEventArgs> afterDataSet)
50         {
51             System.Reflection.PropertyInfo[] pis = entity.GetType().GetProperties();
52             if (pis != null && pis.Length > 0)
53             {
54                 System.Windows.Forms.Control c;
55                 foreach (System.Reflection.PropertyInfo pi in pis)
56                 {
57                     c = null;
58                     try
59                     {
60                         c = parent.Controls.Find(pi.Name, true)[0];
61                     }
62                     catch
63                     {
64                         continue;
65                     }
66                     if (c != null)
67                     {
68                         if ((c is System.Windows.Forms.ComboBox) && !(c as System.Windows.Forms.ComboBox).Items.Contains(pi.GetValue(entity, null))) {
69                             continue;
70                         }
71                         c.Text = pi.GetValue(entity, null).ToString();
72                         if (afterDataSet != null)
73                         {
74                             afterDataSet(new EntityDataBindEventArgs() { CurrentEntity = entity, PropertyInfo = pi,BindControl=c });
75                         }
76                     }
77                 }
78             }
79         }
80     }

此封装要结合之前发的那篇分页控件的文章

逻辑处理不是最终形态 仅仅是临时解决方案

 

posted @ 2013-07-27 18:08  哈哈好玩  阅读(905)  评论(0编辑  收藏  举报