c# - Why is DependencyProperty null after binding? -


after bind reportsettings settingsproperty expect able access reportsettings through settings property. when try nullreferenceexception. can me out here?

reportsettings class implementing inotifypropertychanged.

public mywindow() {     initializecomponent();     // load default template     string defaulttemplatename = "default";     foreach (var reportsettings in settingscache.instance.allreportsettings) {         if (reportsettings.templatename.equals(defaulttemplatename, stringcomparison.currentcultureignorecase)) {             var binding = new binding("settingsproperty") { source = reportsettings };             setbinding(settingsproperty, binding);             var testdirect = reportsettings.isvisible; // ok             var testdepprop = settings.isvisible; // nullreferenceexception!!!             //...         }     } }  public reportsettings settings {     { return (reportsettings)getvalue(settingsproperty); }     set { setvalue(settingsproperty, value); } }  public static readonly dependencyproperty settingsproperty = dependencyproperty.register(         "settings", typeof(reportsettings), typeof(mywindow),         new propertymetadata(null, onsettingschanged)); 

update: in example can use local variable, example easy illustration of problem. problem occurs after construction, access uninitialized 'settings'.

it's because data binding executed asynchronously on lower priority thread. after hook binding, code continue executing before binding gets update value, hence still being null.

you can either use local reportsettings value (as discovered), or use dispatcher.invoke() execute follow-up code, using dispatcherpriority lower dispatcherpriority.databind.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -