黑马程序员技术交流社区

标题: WPF的ListBox绑定数据同步问题 [打印本页]

作者: 朱京辉    时间: 2013-4-2 20:48
标题: WPF的ListBox绑定数据同步问题
本帖最后由 朱京辉 于 2013-4-6 15:41 编辑

窗体功能实现: 两个ListBox, 左边的是源, 右边的是目标, 中间四个按钮, 实现单个移动, 实现全部移动
出现的问题: 点击移动按钮, ListBox的绑定的数据源sourceList的包含的对象数目已经改变了, 但是屏幕上没改变.
简单分析: 感觉和之前老师说的DataContent绑定差不多, 但那个使用了INotifyPropertyChanged接口, 解决了问题, 那这个怎么解决??
附件是界面
  1. //SourceListBox绑定的list集合
  2.         List<Person> sourceList = new List<Person>();
  3.         //AimListBox绑定的list集合
  4.         List<Person> aimList = new List<Person>();

  5.         private void Window_Loaded(object sender, RoutedEventArgs e)
  6.         {
  7.             //初始化SourceListBox的内容
  8.             sourceList.Add(new Person() { Name = "朱京辉", Age = 18 });
  9.             sourceList.Add(new Person() { Name = "李白", Age = 18 });
  10.             //SourceListBox绑定数据
  11.             lbSource.ItemsSource = sourceList;
  12.             //AimListBox绑定数据
  13.             lbAim.ItemsSource = aimList;
  14.         }

  15.         private void btnSouToAim_Click(object sender, RoutedEventArgs e)
  16.         {
  17.             Person selectItem = (Person)lbSource.SelectedItem;
  18.             if (selectItem == null)
  19.             {
  20.                 MessageBox.Show("没有选中相应项");
  21.             }
  22.             else
  23.             {
  24.                 sourceList.Remove(selectItem);
  25.                 aimList.Add(selectItem);
  26.                 //下面重新绑定后也无法刷新数据
  27.                 lbSource.ItemsSource = sourceList;
  28.                  lbAim.ItemsSource = aimList;
  29.             }
  30.         }
复制代码

1.JPG (17.37 KB, 下载次数: 28)

1.JPG

作者: 何红卫    时间: 2013-4-3 11:56
aimList要实现INotifyPropertyChanged接口
作者: 朱京辉    时间: 2013-4-4 15:39
何红卫 发表于 2013-4-3 11:56
aimList要实现INotifyPropertyChanged接口

aimList 是C#的LIST, 难道可以去修改增加接口吗? 应该不可以吧.
老师的例子,因为引用的自己定义的类,所以可以实现接口, 可现在不是啊
作者: 胡化敏    时间: 2013-4-5 19:49
//SourceListBox绑定的list集合

        List<Person> sourceList = new List<Person>();

        //AimListBox绑定的list集合

        List<Person> aimList = new List<Person>();


        private void Window_Loaded(object sender, RoutedEventArgs e)

        {

            //初始化SourceListBox的内容

            sourceList.Add(new Person() { Name = "朱京辉", Age = 18 });

            sourceList.Add(new Person() { Name = "李白", Age = 18 });

            //SourceListBox绑定数据

            lbSource.ItemsSource = sourceList;

            //AimListBox绑定数据

            lbAim.ItemsSource = aimList;

        }


        private void btnSouToAim_Click(object sender, RoutedEventArgs e)

        {

            Person selectItem = (Person)lbSource.SelectedItem;

            if (selectItem == null)

            {

                MessageBox.Show("没有选中相应项");

            }

            else

            {

                sourceList.Remove(selectItem);

                aimList.Add(selectItem);
        // 当你集合发生改变时,是否要重新刷新下各个数据源
            lbSource.ItemsSource = sourceList;
            lbAim.ItemsSource = aimList;

            }

        }
作者: 朱京辉    时间: 2013-4-6 15:46
胡化敏 发表于 2013-4-5 19:49
//SourceListBox绑定的list集合

        List sourceList = new List();

这样的修改是不行的,这里是数据绑定控件,结果没变
作者: 朱京辉    时间: 2013-5-29 08:04
最后找到解决的方法了,使用ObservableCollection<T>集合,因为这个集合实现了INotifyPropertyChanged接口




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2