mvvm - WPF binding does not work with a UserControl -
in mainviewmodel
have property, set user. when new value received, update information @ usercontrol
. problem binding not work. way work following. after selectedday
changed, day
property in user control code behind changes, invoking calculateday
method. however, method never called!
i guess, reason simple , not understand binding mechanism, cannot figure out wrong , need help. have checked number of similar questions, never found appropriate answer.
the code have:
mainviewmodel
private day _selectedday; public day selectedday { { return _selectedday; } set { _selectedday = value; raisepropertychanged("selectedday"); } }
mainwindow xaml
<controls:daycontrol day="{binding selectedday, mode=twoway}"></controls:daycontrol>
usercotrol code behind
public day day { { return (day)getvalue(dayproperty); } set { setvalue(dayproperty, value); calculateday(); } } public static readonly dependencyproperty dayproperty = dependencyproperty.register("day", typeof(day), typeof(daycontrol), new propertymetadata(new day()));
according msdn
in exceptional circumstances, wrapper implementations should perform getvalue , setvalue actions, respectively. reason discussed in topic xaml loading , dependency properties.
you should use propertychangedcallback in definition dependency property instead. following code directly taken msdn.
public static readonly dependencyproperty aquariumgraphicproperty = dependencyproperty.register( "aquariumgraphic", typeof(uri), typeof(aquariumobject), new frameworkpropertymetadata(null, frameworkpropertymetadataoptions.affectsrender, new propertychangedcallback(onurichanged) ) );
define calculateday in place of onurichanged above.
Comments
Post a Comment