WPF中关于ListBox绑定数据的问题
来源:互联网 发布:淘宝刷销量排名靠后 编辑:程序博客网 时间:2024/06/05 06:44
在定义ListBox的ItemSource绑定时,出现了初次绑定有数据,但是数据变化时视图没有更新的情况。
错误发生时代码如下:
//尝试一private void binding(){ List<string> m_allpaths = new List<string>(); System.Windows.Data.Binding newBinding = new System.Windows.Data.Binding(); newBinding.Source = m_allpaths; lsb1.SetBinding(System.Windows.Controls.ListBox.ItemsSourceProperty,newBinding); }//尝试二,定义了一个类,此类中有个字段getAllPath为List<string>类型 public class AllPaths : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private ObservableCollection<string> m_allpaths = new ObservableCollection<string>(); public ObservableCollection<string> getAllPaths { get { return m_allpaths; } set { m_allpaths = value; if (PropertyChanged != null) { this.PropertyChanged.Invoke(this,new PropertyChangedEventArgs("getAllPaths")); } } } }private void binding(){ AllPath m_allpaths = new AllPath(); System.Windows.Data.Binding newBinding = new System.Windows.Data.Binding(); newBinding.Source = m_allpaths; newBinding.Path = new PropertyPath("getAllPaths"); lsb1.SetBinding(System.Windows.Controls.ListBox.ItemsSourceProperty,newBinding); }
发现尝试一中的m_allpaths和尝试二中的getAllPath进行添加时,ListBox的视图均为刷新。百度之后,了解到一个问题,就是对于List< T >这类泛型,视图层是不能够获取到其数据更新的。在comobox和listbox等空间中,其item都是多组字符,因此需要用到以下ObservableCollection< T >,msdn中有对ObservableCollection的介绍:http://msdn.microsoft.com/zh-cn/magazine/dd252944.aspx。
只要将尝试二中的List< string >改成ObservableCollection< string >,即可绑定,也可以参照下面的代码:
private ObservableCollection<T> _default = new ObservableCollection<T>(); public ObservableCollection<CardViewModel> DeafultPro { get { return _default; } set { if (_default != value) { _default = value; RaisePropertyChanged("DeafultPro"); } } }
0 0
- WPF中关于ListBox绑定数据的问题
- WPF中,ListBox与数据间的绑定
- WPF ListBox数据绑定
- 关于LISTBOX绑定数据再刷新的问题
- WPF: ListBox绑定xml数据
- WinForm中ListBox数据绑定问题
- ListBox Item数据绑定遇到的问题
- WPF中ListBox的创建和多种绑定用法
- WPF中ListBox的创建和多种绑定用法
- WPF中ListBox的创建和多种绑定用法
- ListBox的数据绑定
- WPF绑定ListBox
- ListBox控件的数据绑定
- ListBox控件的数据绑定
- WPF combox数据绑定的问题
- c# ListBox绑定对象时删除数据的问题
- WPF listbox异步绑定数据IsAsync=True
- winform里listbox数据绑定,listbox取值问题
- 关于 tomcat 集群中 session 共享的三种方法
- 【Android7.1.2源码解析系列】android中init.rc文件的语法
- 基于GUI的多线程文件传输
- 对进程地址空间的一点认识
- 【Hibernate】该和谁一样 ------ 一个错误下的小尝试
- WPF中关于ListBox绑定数据的问题
- 新书预售《Tomcat内核设计剖析》
- ScrollView嵌套webvView出现空白
- redis主从集群搭建及容灾部署(哨兵sentinel)
- DSO、CUBE对值的加载方式(覆盖、合计)
- 海量数据处理
- 堆和栈的区别【转过无数次的文章】
- csu——最短上学路
- angularjs实现导出excel