none
请教一个关于BindingList绑定到Winform Listbox上后ListBox中Item更新的问题 RRS feed

  • 问题

  • 大家好,有个问题需要请教,问题描述如下:
      首先有一个类:
      public class User
        {
            private string _Name;
            public string Name
            {
                get { return _Name; }
                set {_Name = value;}
            }
        }
      然后我定义了一个BindingList<User>变量list【向list中添加了很多User实例,代码省略了】,并将list绑定到一个ListBox上
       this.listBox1.DataSource = list;
       this.listBox1.DisplayMember = "Name";
       this.listBox1.ValueMember = "Name";

       同时我为了在textBox1中同步显示listBox1中选中的UserName,我为textBox1添加了绑定,如下
       this.textBox1.DataBindings.Add("Text", list, "Name");

       以上功能实现没有问题,现在我想要在更改textBox1中的内容【后】 ,listBox1中当前项也同步更新,请问如何实现。
       现在更改TextBox中的内容【后】 ,list中的数据已经更新,但listbox1显示内容没有更新,不知道为什么。

    环境:VS2008 + Vista + .NET framework 2.0 +Winform
    2009年7月22日 2:53

答案

  • 使用 BindingSource 
    -------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication4
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                List<User> list = new List<User>();
    
                list.Add(new User("Sam"));
                list.Add(new User("Miki"));
                list.Add(new User("John"));
    
                BindingSource fBindingSource = new BindingSource();
                fBindingSource.DataSource = list;
    
                this.listBox1.DataSource = fBindingSource;
                this.listBox1.DisplayMember = "Name";
                this.listBox1.ValueMember = "Name";
    
                this.textBox1.DataBindings.Add("Text", fBindingSource, "Name");
            }
        }
    
        public class User
        {
            private string _Name;
            public string Name
            {
                get { return _Name; }
                set { _Name = value; }
            }
    
            public User(string fName)
            {
                this._Name = fName;
            }
        }
    }
    


    知识改变命运,奋斗成就人生!
    2009年7月22日 5:17
    版主
  • 在对象上实现IEditableObject 和INotifyPropertyChanged

    Please mark the post answered your question as the answer, and click the chartreuse pyramid floating over "Vote as helpful" to mark other helpful posts as helpful. This posting is provided "AS IS" with no warranties, and confers no rights.
    Visual C++ MVP
    2009年7月22日 18:19
    版主

全部回复

  • 使用 BindingSource 
    -------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication4
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                List<User> list = new List<User>();
    
                list.Add(new User("Sam"));
                list.Add(new User("Miki"));
                list.Add(new User("John"));
    
                BindingSource fBindingSource = new BindingSource();
                fBindingSource.DataSource = list;
    
                this.listBox1.DataSource = fBindingSource;
                this.listBox1.DisplayMember = "Name";
                this.listBox1.ValueMember = "Name";
    
                this.textBox1.DataBindings.Add("Text", fBindingSource, "Name");
            }
        }
    
        public class User
        {
            private string _Name;
            public string Name
            {
                get { return _Name; }
                set { _Name = value; }
            }
    
            public User(string fName)
            {
                this._Name = fName;
            }
        }
    }
    


    知识改变命运,奋斗成就人生!
    2009年7月22日 5:17
    版主
  • 在对象上实现IEditableObject 和INotifyPropertyChanged

    Please mark the post answered your question as the answer, and click the chartreuse pyramid floating over "Vote as helpful" to mark other helpful posts as helpful. This posting is provided "AS IS" with no warranties, and confers no rights.
    Visual C++ MVP
    2009年7月22日 18:19
    版主