A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 朱京辉 中级黑马   /  2013-4-2 20:48  /  4712 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 朱京辉 于 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, 下载次数: 26)

1.JPG

评分

参与人数 1技术分 +1 收起 理由
杞文明 + 1

查看全部评分

5 个回复

倒序浏览
aimList要实现INotifyPropertyChanged接口
回复 使用道具 举报
何红卫 发表于 2013-4-3 11:56
aimList要实现INotifyPropertyChanged接口

aimList 是C#的LIST, 难道可以去修改增加接口吗? 应该不可以吧.
老师的例子,因为引用的自己定义的类,所以可以实现接口, 可现在不是啊
回复 使用道具 举报
//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;

            }

        }

评分

参与人数 1技术分 +1 收起 理由
苏波 + 1

查看全部评分

回复 使用道具 举报
胡化敏 发表于 2013-4-5 19:49
//SourceListBox绑定的list集合

        List sourceList = new List();

这样的修改是不行的,这里是数据绑定控件,结果没变
回复 使用道具 举报
最后找到解决的方法了,使用ObservableCollection<T>集合,因为这个集合实现了INotifyPropertyChanged接口
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马