首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

Wpf中ListView的数据绑定的更新同步有关问题(后台绑定数据更新)

2012-10-21 
Wpf中ListView的数据绑定的更新同步问题(后台绑定数据更新)C# code public partial class MainWindow : Wi

Wpf中ListView的数据绑定的更新同步问题(后台绑定数据更新)

C# code
 public partial class MainWindow : Window    {        public class Customer:INotifyPropertyChanged  //定义的类,用于添加到List        {            public string name;            public string imageUrl;            public string sex;            public string age;             public string Name             {                 get { return name; }                 set { name = value; OnPropertyChanged(new PropertyChangedEventArgs("Name")); }            }            public string ImageUrl            {                get { return imageUrl ;}                set { imageUrl = value; OnPropertyChanged(new PropertyChangedEventArgs("ImageUrl")); }             }            public string Sex             {                get { return sex;}                set { sex = value; OnPropertyChanged(new PropertyChangedEventArgs("Sex")); }             }            public string Age             {                get { return age;}                set { age = value; OnPropertyChanged(new PropertyChangedEventArgs("Age")); }             }            #region INotifyPropertyChanged 成员             public event PropertyChangedEventHandler PropertyChanged;            public void OnPropertyChanged(PropertyChangedEventArgs e)            {                if (PropertyChanged != null)                {                   PropertyChanged(this, e);                }            }            #endregion        }        List<Customer> listCustomer = new List<Customer>();  //数据源        public MainWindow() //构造函数        {            this.InitializeComponent();            listCustomer.Add(new Customer() { ImageUrl = "Pic/P_1.png", Name = "hehe1", Sex = "男", Age = "12" });            listview.DataContext = listCustomer;     //此处添加绑定          }

效果如图:


上图足以证明绑定成功!!!但问题来了。。。。。。。。。。。。。。。。。。。。。
有如下按钮事件
C# code
        private void button1_Click(object sender, RoutedEventArgs e)        {              listCustomer.Add(new Customer() { ImageUrl = "Pic/P_5.png", Name = "hehe2", Sex = "女", Age = "16" });             }

意思就是:再通过按钮动态添加一个新的纪录
结果:经测试,listCustomer中确实多了一个记录,但程序界面中却没有更新,也就是没有达到我想要的如下效果:

前提是:我已经在xaml文件中确确实实添加了绑定,请问这个如何解决???还需要什么代码吗?

难道是绑定了listview.DataContext = listCustomer; 之后的改变就不行了???还是学艺不精。。。望高手指教,有代码
帮忙解决最好不过!!!




[解决办法]
用 ObservableCollection<T> 类 或 BindingList<T> 类 代替 List 类

热点排行