WPF Binding inotifypropertychanged Multithreading in-depth understanding

Source: Internet
Author: User

    • Example

First look at an example

Person.cs

 Public classperson:observableobject,inotifypropertychanged {Private string_testname; Privateobservablecollection<string> _names=Newobservablecollection<string>();  Public stringtestname {Get            {                return_testname; }            Set{_testname=value;            OnPropertyChanged (); }        }         Publicobservablecollection<string>Names {Get{return_names;} Set{_names=value; raisePropertyChanged (()=Names); }        }         Public EventPropertyChangedEventHandler propertychanged; protected Virtual voidOnPropertyChanged ([Callermembername]stringPropertyName =NULL) {PropertyChangedEventHandler handler=propertychanged; if(Handler! =NULL) {Handler ( This,NewPropertyChangedEventArgs (PropertyName)); }        }    }

MainWindow.xaml.cs

 Public Partial classMainwindow:window, INotifyPropertyChanged {Private int_currentid; PrivatePerson _person;  PublicPerson person {Get            {                return_person; }            Set{_person=value;            OnPropertyChanged (); }        }         PublicMainWindow () {InitializeComponent ();  This. DataContext = This; person=NewPerson (); Person.testname="testname"; PERSON.NAMES.ADD ("string"); _currentid=Thread.CurrentThread.ManagedThreadId;        Dispatcherhelper.initialize (); }         Public EventPropertyChangedEventHandler propertychanged; protected Virtual voidOnPropertyChanged ([Callermembername]stringPropertyName =NULL) {PropertyChangedEventHandler handler=propertychanged; if(Handler! =NULL) {Handler ( This,NewPropertyChangedEventArgs (PropertyName)); }        }        Private voidButtonbase_onclick (Objectsender, RoutedEventArgs e) {Task.Factory.StartNew ()=            {                if(_currentid = =Thread.CurrentThread.ManagedThreadId) { person=NewPerson (); }                Else                {                    //do you think the attributes can be updated to the interface? Person.testname ="Notsame"; //What about the collection? PERSON.NAMES.ADD ("Hello");        }            }); }    }

Note where the notes are

The result is that the TestName property can be correctly updated to the UI, and the collection properties names not (there is really no understanding, asking for it).

The rest of the understanding, there is an article well written

Http://www.cnblogs.com/wpcockroach/p/3909081.html

WPF Binding inotifypropertychanged Multithreading in-depth understanding

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.