c# - Why would I get a format exception when updating a boolean binding with WriteValue? -


i have bunch of checkboxes on form checked properties bound boolean properties on data model:

chk1.databindings.add(new bindingvalue(this, "checked", "mybooleanproperty1", false)) chk2.databindings.add(new bindingvalue(this, "checked", "mybooleanproperty2", false)) chk3.databindings.add(new bindingvalue(this, "checked", "mybooleanproperty3", false)) 

there shared event handler checkboxes on screen ensures databound value correctly set checked value.

private void allcheckboxes_checkedchanged(object sender, eventargs e) {     var chk = ((checkbox)sender);      var binding = chk.databindings["checked"];     if (binding != null)         binding.writevalue(); } 

in cases, first time form , bindings loaded, exception :

cannot format value desired type.

at system.componentmodel.reflectpropertydescriptor.setvalue(object component, object value) @ system.windows.forms.bindtoobject.setvalue(object value) @ system.windows.forms.binding.pulldata(boolean reformat, boolean force) @ system.windows.forms.binding.writevalue()

it works correctly first checkbox process event, second 1 throw exception.

the data source interface of datamodel

public interface imydatamodel {     bool mybooleanproperty1 { get; set; }     bool mybooleanproperty2 { get; set; }     bool mybooleanproperty3 { get; set; } } 

and can verify data model set correctly setting breakpoint right before .writevalue in event handler. can put breakpoint in setter of bound boolean property, , called correctly.

if set formattingenabled property of binding true, fix issue. wondering why have in first place, since binding system.boolean property in ui object bool property on data source.

why getting exception in case?

it's difficult sure what's going on without way reproduce issue.

but @ least can explain why setting formattingenabled makes exception go away:

http://referencesource.microsoft.com/#system.windows.forms/winforms/managed/system/winforms/binding.cs,853

writedata calls pulldata reformat = true , force = true.

from call stack, looks must making block:

            // put value data model             if (!parsefailed) {                 this.bindtoobject.setvalue(parsedvalue);             } 

immediately following that, exception rethrown unless formattingenabled = true. setting formattingenabled here hides problem, since seems binding assumes you're going handle formatting/parsing issues it's found far yourself.

as why exception thrown in first place... don't know. nothing obvious looking here:

http://referencesource.microsoft.com/#system/compmod/system/componentmodel/reflectpropertydescriptor.cs,1138

i'd curious know if problem persists if find way add data bindings without bindingvalue custom class. i'd curious if have listeners wired in bindingvalue.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -