代码改变世界

数据Binding一(实体类的Binding)

2007-06-22 16:47  老羽  阅读(615)  评论(1编辑  收藏  举报

通过一个例子讲讲实体类是如何实现和Form中的控件Binding的。

先看这个简单的实体类。INotifyPropertyChanged  实现当属性被修改时,通知被binding的其它对象,向客户端发出某一属性值已更改的通知

 

public class NodeInfo : INotifyPropertyChanged    {

        public NodeInfo(string key, string content)

        {

            this._key = key;

            this._content = content;

        }

        public NodeInfo()

        {

        }

 

        private string _key;

        private string _content;

 

        [DisplayName("主键")]  //自定义显示标题

        public string Key

        {

            get

            {

                return this._key;

            }

            set

            {

                if (PropertyChanged != null)

                {

                    this._key = value;

                    NotifyPropertyChanged("Content");

                }

            }

        }

 

        [DisplayName("内容")]

        public string Content

        {

            get { return this._content; }

            set

            {

                if (PropertyChanged != null)

                {

                    this._content = value;

                    NotifyPropertyChanged("Content");

                }

            }

        }

 

        #region Method

        private void NotifyPropertyChanged(String propertyName)

        {

            if (PropertyChanged != null)

            {

                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

            }

        }

 

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

}

这里[DisplayName("主键")]是用于显示自定义的标题,且看下面的代码(省略生成控件的代码),如果不加入此Attribute,则以属性的名称为标题.

public partial class Form2 : Form

    {

        #region Fields

        private BindingSource _bs;

        private BindingList<NodeInfo> _bindList;

        #endregion

        public Form2()

        {

            InitializeComponent();

            //

            InitDataSource();

            InitDataBinding();

        }

 

        #region Method

        private void InitDataSource()

        {

            _bindList = new BindingList<NodeInfo>();

            _bindList.Add(new NodeInfo("001","text001"));

            _bindList.Add(new NodeInfo("002","text002"));

            _bindList.Add(new NodeInfo("003","text003"));

 

            this._bs=new BindingSource();

            this._bs.DataSource=this._bindList;

 

            //测试DataGridView,BindingNavigator

            this.dataGridView1.DataSource = this._bs;

            this.bindingNavigator1.BindingSource = this._bs;

 

            //测试ComboBox

            this.comboBox1.DataSource = this._bs;

            this.comboBox1.DisplayMember = "Content";

            this.comboBox1.ValueMember = "Key";

        }

 

        private void InitDataBinding()

        {

            this.textBox1.DataBindings.Add("Text", this._bs, "Key");

            this.textBox2.DataBindings.Add("Text", this._bs, "Content");

        }

        //测试读取对象

        private void button1_Click(object sender, EventArgs e)

        {

            NodeInfo node = this._bs.Current as NodeInfo;

            if (node != null)

            {

                MessageBox.Show(node.Key + "\r\n" + node.Content);

            }

        }

        //测试写入对象

        private void button2_Click(object sender, EventArgs e)

        {

            _bindList[0].Content = "test content changed";

        }

 

        #endregion

}

运行界面如下:

      


效果和Binding DataTable 类似, BindingNavigator控件的移动,新增,删除都可以使用;移动ComoBox的选项时,TextBox,DataGridView中的项也随之变化;移动DataGridView中的选择项时,TextBox,ComoBox的项也随之变化;但是DataGridView没有了排序的功能,因为没有Binding DataTableDataView等数据源的原因。

不难看出,直接对实体类Binding的效果没有很大影响,对不喜欢用DataSet的朋友来说,是好事,避免了直接对控件赋值,提高不少效率。下一篇将讲关于Binding的内部实现,为什么不同的控件都可以关联同一项,也讲一些自深的体会,及开发中碰到的问题。