类维护一个arraylist,如何把arraylist绑定到listbox上?希望arraylist里内容变化时,listbox上能相应变化。
            list = new ArrayList();
            listBox1.DataSource = list;
            listBox1.DataBind();   
我像上面这么用,显示:System.Windows.Controls.ListBox' does not contain a definition for 'DataSource'能通过xaml将lixtbox item绑定到一个类的集合吗?谢谢!

解决方案 »

  1.   

    ItemsSource={Binding},在代码中将listBox的datacontext设为绑定源,另外arraylist可能不会自动更新,要实现某个接口,用obs******的那个集合可以自动更新
      

  2.   

    为了让目标述性与源集合的更改保持同步,源集合必须实现一个叫INotifyCollectionChanged的接口。
    所以我不确定是否listbox会和arraylist保持同步
      

  3.   

    arraylist不会保持数据同步的,用 ObservableCollection<T>来实现。
    同时,Model类的属性,要使用DependencyProperty,如:
     public static readonly DependencyProperty StudentIpProperty =
                DependencyProperty.Register("StudentPcIp", typeof(string),
                typeof(StudentInfo), new UIPropertyMetadata(null));
            /// <summary>
            /// 客户端地址
            /// </summary>
            public string StudentPcIp
            {
                get { return (string)GetValue(StudentIpProperty); }
                set { SetValue(StudentIpProperty, value); _studentPcIpNoDependencyProperty = value; }
            }
    这样,WPF才会自动关联更新