编辑控件
public abstract partial class TEditorBase <TEntity, TRepository, TSqlStrConstruct> : UserControl
where TEntity:Yom.Extend.Entity.EntityBase
where TRepository : Yom.Extend.Repository.RepositoryBaseRepository<TEntity, TSqlStrConstruct>
where TSqlStrConstruct : Huawei.Data.SqlStrConstruct
{
protected TRepository repository = System.Activator.CreateInstance<TRepository>();
public TEditorBase()
{
InitializeComponent(); this.gpxTitle.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
}
string key;
public string Key
{
get {
return key;
}
set {
if (string.IsNullOrEmpty(value)) {
return;
}
key = value;
Entity = this.repository.FindBy(value) as Yom.Extend.Entity.EntityBase;
}
}
protected abstract Action<EntityDataBindEventArgs> AfterMapDataToEntityAction
{
get;
}
protected abstract Action<EntityDataBindEventArgs> AfterMapDataToControlAction
{
get;
}
protected virtual Yom.Extend.Entity.EntityBase Entity
{
get {
TEntity entity = System.Activator.CreateInstance<TEntity>();
entity.PropertyValueSet(this.repository.GetEntityKeyName(), this.Key);
DataDetailGetFromControlChildren.MapDataToEntity(entity, this, AfterMapDataToEntityAction);
return entity;
}
set {
if (value == null)
{
return;
} DataDetailGetFromControlChildren.MapDataToControl(value, this, this.AfterMapDataToControlAction);
}
} protected virtual string Title
{
get {
return string.Empty;
}
}
protected virtual bool IsValid(out string msg)
{
msg = string.Empty;
return true;
}
protected virtual void SaveCallBack()
{
string msg = string.Empty;
if (!IsValid(out msg))
{
MessageBox.Show(msg);
return;
}
try
{
if (string.IsNullOrEmpty(this.Key))
{
do
{
this.key = Guid.NewGuid().ToString();
} while (this.repository.Exists(this.key));
this.repository.Add(this.Entity);
}
else
{
this.repository.Update(this.Entity);
}
MessageBox.Show("保存成功!");
this.Hide();
this.Parent.Controls.Add(Lister);
this.Parent.Controls.Remove(this);
this.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(string.Format("保存失败:\r\n{0}", ex.Message));
}
} protected virtual void CancelCallBack()
{
//this.Hide();
//this.Parent.Controls.Add(GetListControl());
//this.Parent.Controls.Remove(this);
//this.Dispose();
//this = null;
if (this.Parent.Controls.Count <= )
{
this.Parent.Controls.Add(this.Lister);
this.Parent.Controls.RemoveAt();
}
else
{
this.Parent.Controls[].Show();
this.Parent.Controls.RemoveAt();
}
} private void bSave_Click(object sender, EventArgs e)
{
SaveCallBack();
} private void TEditorBase_Load(object sender, EventArgs e)
{
this.gpxTitle.Text = this.Title;
}
public abstract TListPager<TEntity, TRepository, TSqlStrConstruct> Lister
{
get;
} private void bCancel_Click(object sender, EventArgs e)
{
CancelCallBack();
}
}
属性赋值:
public class EntityDataBindEventArgs : EventArgs
{ public Yom.Extend.Entity.EntityBase CurrentEntity
{
get;
set;
}
public System.Reflection.PropertyInfo PropertyInfo
{
get;
set;
}
public System.Windows.Forms.Control BindControl
{
get;
set;
}
}
public class DataDetailGetFromControlChildren
{
public static void MapDataToEntity(Yom.Extend.Entity.EntityBase entity, System.Windows.Forms.Control parent, Action<EntityDataBindEventArgs> afterDataGet)
{ System.Reflection.PropertyInfo[] pis = entity.GetType().GetProperties();
if (pis != null && pis.Length > ) {
System.Windows.Forms.Control c;
foreach (System.Reflection.PropertyInfo pi in pis)
{
c = null;
try
{
c = parent.Controls.Find(pi.Name, true)[];
}
catch {
continue;
}
if (c != null)
{
entity.PropertyValueSet(pi.Name, c.Text);
if (afterDataGet != null)
{
afterDataGet(new EntityDataBindEventArgs() { CurrentEntity = entity, PropertyInfo = pi, BindControl = c });
}
}
}
}
}
public static void MapDataToControl(Yom.Extend.Entity.EntityBase entity, System.Windows.Forms.Control parent, Action<EntityDataBindEventArgs> afterDataSet)
{
System.Reflection.PropertyInfo[] pis = entity.GetType().GetProperties();
if (pis != null && pis.Length > )
{
System.Windows.Forms.Control c;
foreach (System.Reflection.PropertyInfo pi in pis)
{
c = null;
try
{
c = parent.Controls.Find(pi.Name, true)[];
}
catch
{
continue;
}
if (c != null)
{
if ((c is System.Windows.Forms.ComboBox) && !(c as System.Windows.Forms.ComboBox).Items.Contains(pi.GetValue(entity, null))) {
continue;
}
c.Text = pi.GetValue(entity, null).ToString();
if (afterDataSet != null)
{
afterDataSet(new EntityDataBindEventArgs() { CurrentEntity = entity, PropertyInfo = pi,BindControl=c });
}
}
}
}
}
}
此封装要结合之前发的那篇分页控件的文章
逻辑处理不是最终形态 仅仅是临时解决方案