使用双向绑定动态编辑任意对象内容(一)

      在上篇<使用反射简化日常操作>讲到了在ASP.NET下,运用反射进行对象内容编辑的方法。在这里介绍一种在WinForm下,进行简单对象内容编辑的办法。
      相对而方,WinForm可使用方法更多,且更为简单。在System.Windows.Forms下,有一个简单而有效的对象:Binding,运用它可以进行对象属性值与控件属性之间进行绑定。只要设置了绑定,两个对象的两个属性的值就会连动起来,所以我说它是一个双向绑定。
      运用绑定的方式非常简单,任意拖拉个控件都拥有这个功能,因为System.Windows.Forms.Control就已支持它。能过UI的方式进行设置绑定,功能强大而简单,但使用代码的方式也非常方便,如:

//editCtrl          代表与对象进行绑定的控件
//bindName      代表editCtrl的一个属性名
//EditObject      代表进行进行绑定的对象
//name             代表EditObject的属性名
editCtrl.DataBindings.Add(new Binding(bindName, EditObject, name));


      上面的代码很简单明了,而这里也是围绕这个小小的代码展开。
      由于是对任意对象进行编辑,所以不预定义任意进行编辑的对象,但会定义一个用于指导动态造成控件,提供数据源的辅助对象。
      字段说明特性(用于为编辑的属性定义中文名,是否只读)

using System;
using System.Collections.Generic;
using System.Text;

namespace Guaik
{
     /// 
     /// 字段说明
   /// 
    public class ColumnDescriptionAttribute : System.Attribute
    {
        public ColumnDescriptionAttribute(string description, bool readOnly)
        {
            _Description = description;
            _ReadOnly = readOnly;
        }

        public ColumnDescriptionAttribute(bool readOnly)
            : this(null, readOnly)
        {
        }

        public ColumnDescriptionAttribute(string description)
            : this(description, false)
        {
        }

        private string _Description;
        /// 
        /// 说明
     /// 
        public string Description
        {
            get
            {
                return _Description;
            }
        }

        private bool _ReadOnly;
        /// 
        /// 是否只读
     /// 
        public bool ReadOnly
        {
            get
            {
                return _ReadOnly;
            }
        }

    }
}


      数据源特性(定义字段的取值范围)

using System;
using System.Collections.Generic;
using System.Text;

namespace Guaik
{
    /// 
    /// 数据源
   /// 
    public class DataSourceAttribute : System.Attribute
    {
        private Type _EnumType;
        private IList list;
        public IList GetSource()
        {
            if (list != null)
            {
                return list;
            }
            else
            {
                return DataItem.GetDataListByEnum(_EnumType);
            }
        }

        public DataSourceAttribute(IList list)
        {
            this.list = list;
        }

        public DataSourceAttribute(Type enumType)
        {
            _EnumType = enumType;
        }
    }
}


      数据源项对象

using System;
using System.Collections.Generic;
using System.Text;

namespace Guaik
{
    public class DataItem
    {
        private string _name;
        public String Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private object _value;
        public object Value
        {
            get { return _value; }
            set { _value = value; }
        }

        public static IList GetDataListByEnum(Type enumType)
        {
            DataItem di = null;
            Array values = Enum.GetValues(enumType);
            Array names = Enum.GetNames(enumType);
            IList list = new List();
            for (int i = 0; i < values.Length; i++)
            {
                di = new DataItem();
                di.Name = names.GetValue(i).ToString();
                di.Value = values.GetValue(i);
                list.Add(di);
            }
            return list;
        }
    }
}


      隐藏属性特性(为要保护的字段提供保护)

using System;
using System.Collections.Generic;
using System.Text;

namespace Guaik.CodeSmith.Core
{
    /// 
    /// 显示需要隐藏的内容
   /// 
    public class HideAttribute : System.Attribute
    {
    }
}


      至此定义了四个辅助对象,运用这四个对象可以做到为字段指定名称、限定只读、限定数据范围和为特定数据提供保护。在下一篇中,将会介绍如何运用这四个对象为任意对象动态生成控件和进行双向绑定的过程。

相关文章:
      使用双向绑定动态编辑任意对象内容(二)
      使用反射简化日常操作

Tag标签: 反射 绑定
发表于 2009-06-26 20:57:37 收藏 所属分类: C# 网摘收藏
相关文章: