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?