The observablecollection<t> class in MSDN represents a Dynamic data collection that provides a notification when an item is added, an item is removed, or the entire list is refreshed.
In many cases, the data used is a collection of objects. For example, a common scenario in data binding is to use a itemscontrol (such as a ListBox, ListView, or TreeView) to display a collection of records.
You can enumerate any collection that implements the IEnumerable interface.inotifycollectionchanged interface. " However, to set up dynamic binding so that the INSERT or delete operation in the collection can automatically update the UI, the collection must implement the INotifyCollectionChanged interface. collectionchanged event, an event that should be raised whenever th E underlying collection changes. " > This interface exposes the CollectionChanged event, which should be raised whenever the underlying collection has changed.
WPF provides the observablecollection<t> class, which is a built-in implementation of a data collection that implements the INotifyCollectionChanged interface.
In many cases, the data we use is simply a field or attribute, and we need to implement the INotifyPropertyChanged interface for those fields or properties, and implement that interface, providing a notification mechanism whenever a field or property has changed.
Instance:
XAML Specifies TwoWay bidirectional binding
<Grid> <stackpanel height= "295" horizontalalignment= "left" margin= "10,10,0,0" name= "StackPanel1" Verticalalignment= "Top" width= "427" > <textblock height= "All" Name= "TextBlock1" text= "learner number:"/> <t Extbox height= "name=" Txtstudentid "width=" 301 "horizontalalignment=" left "/> <textblock Height=" Name= " TextBlock2 "text=" Learner list: "/> <listbox height=" 156 "name=" Lbstudent "width=" 305 "horizontalalignment=" left "> <ListBox.ItemTemplate> <DataTemplate> <stackpanel name= "stack Panel2 "orientation=" horizontal "> <textblock text=" {Binding id,mode=twoway} "margin=" 5 "Back ground= "Beige"/> <textblock text= "{Binding name,mode=twoway}" margin= "5"/> <textblock text= "{Binding age,mode=twoway}" margin= "5"/> </StackPanel> </DataTemplate></ListBox.ItemTemplate> </ListBox> <button content= "button" height= "Name=" Button1 "width=" "Horizontalalignment=" "left" click= "Button1_Click"/> </StackPanel></Grid>
Background code Creation observablecollection<t> Instances:
observablecollection<students> infos = new observablecollection<students> () { new Students () {Id=1, age=11, Name= "Tom"}, new Students () {id=2, age=12, name= "Darren"}, new Students () {id=3, age=13, name= "Jacky"}, New Students () {id=4, age=14, name= "Andy"} }; Public Bind7 () { InitializeComponent (); This.lbStudent.ItemsSource = infos; This.txtStudentId.SetBinding (Textbox.textproperty, New Binding ("Selecteditem.id") {Source = lbstudent});} private void Button1_Click (object sender, RoutedEventArgs e) { infos[1] = new Students () {Id = 4, age = +, Name = "This is A set Change "}; INFOS[2]. Name = "This is a property change";} public class students{Public int Id {get; set;} public string Name {get; set;} public int Age {get; set;}}
Show Results:
Description,observablecollection<t> notification only for list data item modifications, no notification for list Item property modifications
If you want to notify the UI for object property modification, you need the class implementation INotifyPropertyChanged interface
public class students:inotifypropertychanged{ string _name; public int Id {get; set;} public string Name { get {return _name;} set {_name = value; OnPropertyChanged ("Name"); } } public int age {get; set;} protected internal virtual void OnPropertyChanged (String propertyname) { if (propertychanged! = null) PropertyChanged (This, new PropertyChangedEventArgs (PropertyName)); } public event PropertyChangedEventHandler PropertyChanged;}
C # WPF Collection two-way binding