The propertychangedcallback only triggered once?

Source: Internet
Author: User

In your custom user control, add a dependency property, as follows:

        public static readonly DependencyProperty itemssourceproperty =             dependencyproperty.register ("ItemsSource", typeof (Dictionary<string, object>), typeof (Multiselectcombobox), new Frameworkpropertymetadata (NULL,        new Propertychangedcallback (multiselectcombobox.onitemssourcechanged));        Public dictionary<string, object> ItemsSource        {            get {return (dictionary<string, object>) GetValue (Itemssourceproperty); }            set            {                SetValue (itemssourceproperty, value);            }        }        private static void Onitemssourcechanged (DependencyObject D, DependencyPropertyChangedEventArgs e)        {            Multiselectcombobox control = (multiselectcombobox) D;            Control. Displayincontrol ();        }

Using the MVVM pattern, add the control to the views and set the binding value for ItemsSource in ViewModel, as follows:

Views

<control:multiselectcombobox x:name= "Mcwind"                              visibility= "{Binding showwind, Converter={staticresource Booleantovisibilityconverter}} "                             itemssource=" {Binding Itemswind,mode=twoway} "selecteditems=" {Binding Selecteditemswindt, Mode=twoway} "  />

ViewModel:

         Public voidInitwind () {if(Itemswind = =NULL) {Itemswind=Newdictionary<string,Object>();            } itemswind.clear (); varLststation =Getselectedstationids (); varLstmodel =Getselectedmodelids (); foreach(varKvinchDataCache.Instance.Winds) {//Windturbine w = kv. Value as Windturbine;                if(Lststation.contains (KV. Windpowerstationid) &&Lstmodel.contains (KV. ModelID)) {Itemswind.add (kv).                Name, KV); }            }        }

at this point, the problem: in the ViewModel of the Initwind, only the first time Propertychangedcallback successful, and then re-modify Itemswind, the Itemsource in the control is updated, but Propertychangedcallback does not fire. Simply put, the binding of the dependency property succeeds, but the callback function of the property change does not trigger ~

In the StackOverflow, some people seem to have encountered the same situation, but the reply to the Daniel seems to have mistaken situation:

Http://stackoverflow.com/questions/10139475/dependencyproperties-propertychangedcallback-only-called-once

Http://stackoverflow.com/questions/5795770/wpf-propertychangedcallback-triggered-only-once

Many people say that the binding is broken, the cycle of death and so on, there is no suitable solution

In the end, my solution is simple: give a new address (reference) in ViewModel, as follows:

         Public voidInitwind () {Dictionary<string,Object> Dictwind =Newdictionary<string,Object>(); varLststation =Getselectedstationids (); varLstmodel =Getselectedmodelids (); foreach(varKvinchDataCache.Instance.Winds) {if(Lststation.contains (KV. Windpowerstationid) &&Lstmodel.contains (KV. ModelID)) {Dictwind.add (kv).                Name, KV); }} itemswind=Dictwind; }

Waste a large half day debugging, make this note memo ~

The propertychangedcallback only triggered once?

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.