The ObservableCollection of the two-way binding notification mechanism in WPF is used, and the two-way binding of wpf

Source: Internet
Author: User

The ObservableCollection of the two-way binding notification mechanism in WPF is used, and the two-way binding of wpf

The ObservableCollection <T> class in msdn indicates a dynamic data set. When an item is added, removed, or the entire list is refreshed, this set provides notifications.

In many cases, the data used is a collection of objects. For example, a common solution in data binding is to use ItemsControl (such as ListBox, ListView, or TreeView) to display the set of records.

You can enumerate any set that implements the IEnumerable interface. However, to set dynamic binding so that the insertion or deletion operations in the set can automatically update the UI, the set must implement the INotifyCollectionChanged interface. This interface exposes the CollectionChanged event. This event should be triggered if the basic set is changed.

WPF provides the ObservableCollection <T> class, which is a built-in implementation of the data set that implements the INotifyCollectionChanged interface.

In many cases, we only use fields or attributes. At this time, we need to implement the INotifyPropertyChanged interface for these fields or attributes to implement this interface, A notification mechanism is provided as long as the field or attribute changes.

ObservableCollection <T> Implementation

Foreground xmal

<Window x: Class = "WpfApplication1.WindowObservable" xmlns =" http://schemas.microsoft.com/winfx/2006/xaml /Presentation "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "Title =" Window8 "Height =" 356 "Width =" 471 "> <Grid> <StackPanel Height =" 295 "HorizontalAlignment =" Left "Margin =", 10, 427 "Name =" stackPanel1 "verticalignment =" Top "Width =" "> <TextBlock Height =" 23 "Name =" textBlock1 "Text =" student ID: "/> <TextBox Height =" 23 "Name =" txtStudentId "Width =" 301 "HorizontalAlignment =" Left "/> <TextBlock Height =" 23 "Name =" textBlock2 "Text = "student list: "/> <ListBox Height =" 156 "Name =" lbStudent "Width =" 305 "HorizontalAlignment =" Left "> <ListBox. itemTemplate> <DataTemplate> <StackPanel Name = "stackPanel2" Orientation = "Horizontal"> <TextBlock Text = "{Binding Id, mode = TwoWay} "Margin =" 5 "Background =" 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 = "23" Name = "button1" Width = "75" HorizontalAlignment = "Left" Click = "button#click"/> </StackPanel> </Grid> </Window>

Backend cs

Using System; using System. collections. generic; using System. linq; using System. text; using System. windows; using System. windows. controls; using System. windows. data; using System. windows. documents; using System. windows. input; using System. windows. media; using System. windows. media. imaging; using System. windows. shapes; using System. collections. objectModel; using System. componentModel; namespace WpfApplication1 {public partial class WindowObservable: Window {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 WindowObservable () {InitializeComponent (); this. lbStudent. itemsSource = infos; this.txt StudentId. setBinding (TextBox. textProperty, new Binding ("SelectedItem. id ") {Source = lbStudent});} private void button#click (object sender, RoutedEventArgs e) {infos [1] = new Students () {Id = 4, Age = 14, 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 ;}}}}

In this example, the Students data object is modified using ObservableCollection <T>. In this way, when we click, we can see that. After clicking this button, only the change of the entire student object triggers the background notification mechanism.

INotifyPropertyChanged implementation

INotifyPropertyChanged will send a notification to the client that a property value has been changed. When the element property value changes, the background model will be notified.

The front-end code remains unchanged. We have the background Students Model implement the INotifyPropertyChanged interface.

Using System; using System. collections. generic; using System. linq; using System. text; using System. windows; using System. windows. controls; using System. windows. data; using System. windows. documents; using System. windows. input; using System. windows. media; using System. windows. media. imaging; using System. windows. shapes; using System. collections. objectModel; using System. componentModel; namespace WpfApplication1 {pu Blic partial class WindowObservable: Window {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 WindowObservable () {InitializeComponent (); this. lbStudent. itemsSource = infos; this.txt StudentId. setBinding (TextBox. textProperty, new Binding ("SelectedItem. id ") {Source = lbStudent});} private void button#click (object sender, RoutedEventArgs e) {infos [1] = new Students () {Id = 4, Age = 14, name = "this is a set change"}; infos [2]. name = "this is a property change";} 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 ;}}}

Then we run the code to find

Both the set and the object have changed. So far. Our entire background notification can perfectly monitor any object changes.

Code reference: http://blog.csdn.net/fwj380891124/article/details/8194190

Address: http://www.cnblogs.com/santian/p/4366832.html

Blog: http://www.cnblogs.com/santian/

For reprinting, please use hyperlinks to indicate the original source of the article.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.