将列表框绑定到ObservableCollection仅适用于第1项

时间:2013-11-23 11:37:43

标签: c# data-binding listbox observablecollection inotifypropertychanged

我正在尝试创建一个应用程序,将自定义对象(Places)的List / Collection等与列表框绑定在一起,以便:

  • ListBox显示每个对象的选定属性("名称")
  • 选择ListBox中的项目会更改"当前"项目 列表/收藏
  • 在ListBox中添加/删除项目,添加/删除项目 列表/收藏
  • 添加/删除列表/集合中的项目,添加/删除项目 列表框
  • 更改项目的显示属性,更改项目 列表框

到目前为止我的代码是:

namespace DataBindingTest

{

public partial class Form1 : Form

{

    BindingSource bs;
    ObservableCollection<Place> places = new ObservableCollection<Place>();
    Place place1 = new Place("pl1", 2.2);
    Place place2 = new Place("pl2", 2.3);

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        bs = new BindingSource();
        places.Add(place1);
        places.Add(place2);
        bs.DataSource = places;

        listBox1.DataSource = bs;
        listBox1.DisplayMember = "Name";
        listBox1.DataBindings.Add(new Binding("Text", bs, "Name", true, DataSourceUpdateMode.OnPropertyChanged));             
    }

    public class Place : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        string name;
        double losses;

        public Place(string n, double l)
        {
            name = n;
            losses = l;
        }

        public string Name
        {
            get { return name; }
            set 
            {
                name = value;
                OnPropertyChanged("Name");
            }
        }

        public double Losses
        {
            get { return losses; }
            set { losses = value; }
        }

        protected void OnPropertyChanged(string PropertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {

        places[0].Name = "Place 1";
        places[1].Name = "Place 2";
    }
}

OnPropertyChanged调用places[0]时,如果places[1]调用它,则处理程序为空!为什么会这样?

使用ObservableCollection似乎是一个问题(可能是错误?)!如果我使用BindingList,它工作正常!!找到它here。经过3天的搜索。

修改

namespace DataBindingTest
{
    public partial class Form1 : Form
    {
        BindingSource bs;
        BindingList<Place> places = new BindingList<Place>();
        Place place1 = new Place("pl1", 2.2);
        Place place2 = new Place("pl2", 2.3);


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            bs = new BindingSource();
            places.Add(place1);
            places.Add(place2);
            bs.DataSource = places;

            listBox1.DataSource = bs;
            listBox1.DisplayMember = "Name";
            listBox1.DataBindings.Add(new Binding("Text", bs, "Name", true, DataSourceUpdateMode.OnPropertyChanged));

            Place place3 = new Place("pl3", 0);
            bs.Add(place3);
            places[2].Name = "Place3";

        }

        private void button1_Click(object sender, EventArgs e)
        {
            places[0].Name = "Place 1";
            places[1].Name = "Place 2";
        }


    }


    public class Place : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        string name;
        double losses;

        public Place(string n, double l)
        {
            name = n;
            losses = l;
        }


        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                OnPropertyChanged("Name");

            }
        }

        public double Losses
        {
            get { return losses; }
            set { losses = value; }
        }

        protected void OnPropertyChanged(string PropertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(PropertyName));
        }
    }

}

我只是将Class Place放在Class Form1之外,因为我需要在整个namespase中使用它,但我不认为它改变了一些东西。

2 个答案:

答案 0 :(得分:2)

使用ObservableCollection似乎是一个问题(可能是错误?)!如果我使用BindingList,它工作正常!!在这里找到它。经过3天的搜索。

修改

namespace DataBindingTest 
{

    public partial class Form1 : Form

    {

        BindingSource bs;
        BindingList<Place> places = new BindingList<Place>();
        Place place1 = new Place("pl1", 2.2);
        Place place2 = new Place("pl2", 2.3);


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            bs = new BindingSource();
            places.Add(place1);
            places.Add(place2);
            bs.DataSource = places;

            listBox1.DataSource = bs;
            listBox1.DisplayMember = "Name";
            listBox1.DataBindings.Add(new Binding("Text", bs, "Name", true, DataSourceUpdateMode.OnPropertyChanged));

            Place place3 = new Place("pl3", 0);
            bs.Add(place3);
            places[2].Name = "Place3";

        }

        private void button1_Click(object sender, EventArgs e)
        {
            places[0].Name = "Place 1";
            places[1].Name = "Place 2";
        }


    }


    public class Place : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        string name;
        double losses;

        public Place(string n, double l)
        {
            name = n;
            losses = l;
        }


        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                OnPropertyChanged("Name");

            }
        }

        public double Losses
        {
            get { return losses; }
            set { losses = value; }
        }

        protected void OnPropertyChanged(string PropertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(PropertyName));
        }
    }

}

答案 1 :(得分:0)

不确定原因,但似乎只是更新了所选项目。这并不优雅,但它确实可以作为权宜之计解决方案:

private void button1_Click(object sender, EventArgs e)
{
   int actualIndex = listBox1.SelectedIndex;
   listBox1.SelectedIndex = 0;
   places[0].Name = "Place 1";
   listBox1.SelectedIndex = 1;
   places[1].Name = "Place 2";
   listBox1.SelectedIndex = actualIndex;
}