c# - Boolean to Integer converter issue when set from ViewModel -


i have radio button boolean integer converter class:

public class radiobooltointconverter : ivalueconverter {     public object convert(object value, type targettype, object parameter,                           cultureinfo culture)     {         int integer = (int)value;         if (integer == int.parse(parameter.tostring()))             return true;         else             return false;     }      public object convertback(object value, type targettype, object parameter,                                cultureinfo culture)     {                     return parameter;     } } 

i have 2 radio buttons inside stackpanel bound property "testtyperef":

<stackpanel orientation="horizontal">     <radiobutton          content="screening"          margin="0 0 10 0"          ischecked="{binding path=testtyperef, mode=twoway, converter={staticresource radiobooltointconverter}, converterparameter=0 }" />     <radiobutton          content="full"          margin="0 0 10 0"          ischecked="{binding path=testtyperef, mode=twoway, converter={staticresource radiobooltointconverter}, converterparameter=1 }" /> </stackpanel> 

the problem arises when try set value of property radiobuttons bound, in associated viewmodel.

when set value 0 1 - fine.

when set value 1 0 - property value remains 1:

inside viewmodel:

// set default test type. testtyperef = 0; // ~~> testtyperef = 0 testtyperef = 1;   // ~~> testtyperef = 1           testtyperef = 0; // ~~> testtyperef = 1 i.e. no change. 

many can offer.

update: thank both @rachel , @will feedback. convertback routine in error. fixed it:

    return value.equals(false) ? dependencyproperty.unsetvalue : parameter; 

i think may using converter incorrectly, i'm not sure understand original premise.

a converter used convert bound value different type. converter saying when reading property ui ischecked, is

"compare bound value (testtyperef) parameter given. if same, check radio button"

it saying when doing reverse conversion (writing ischecked value bound value (testtyperef),

"send parameter value"

so logic...

when ui binds values read ischecked property

 if testtyperef equal 0      radio 0 checked because value equals parameter (0)     radio 1  unchecked because value not equal parameter (1)  if testtyperef equal 1      radio 0 unchecked because value not equals parameter (0)     radio 1  checked because value equals parameter (1) 

now problem comes in reverse binding, when writes value data source

 if radio 0 checked, write 0 datasource if radio 0 unchecked, write 0 datasource if radio 1 checked, write 1 datasource if radio 1 unchecked, write 1 datasource 

when set datasource value something, triggering update ischecked value, in turn writes datasource

 default : testtyperef = 0     radio 0 checked     radio 1 unchecked  set testtyperef = 1     update radio 0 binding (checked > unchecked)     update radio 1 binding (unchecked > checked)  because ischecked updates, uses convertback update data source.      if radio 1 checked, write 1 datasource 

note @ point, change event radio0 binding doesn't run, because sees nothing has changed - change event raised, value went 1 1, doesn't run code update anything.

you can confirm putting breakpoint in convertback method, , you'll note gets hit value of "1" when try set value 0. breakpoint in setter of testtyperef show changing value first 0, 1 again.

long story short, fix converter. or better yet, fix design.

from can tell, trying "if bound value equal value, ischecked true", statement cannot reverse accurately. if user checks radio button, great set bound value equal parameter. if user unchecks radio button, has no idea set bound value to.

so either

  1. change bindings oneway never run convertback , update data source. not ideal if want users use radio buttons.
  2. change bound value it's formatted in correct way display data, such 2 bool properties called isfull , isscreening can bind directly
  3. implement better set of ui controls this. personal preference listbox bound single value , styled radiobuttons, might overkill bool value.

personally go suggest 2 right now.

public bool isscreening {      { return testtyperef == 0; }     set { if (value) testtyperef = 0; } }  public bool isfull {      { return testtyperef == 1; }     set { if (value) testtyperef = 1; } } 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -