i trying bind visibility property of textboxes in stackpanel ischecked property of checkbox in wpf using mvvm. so, have created property, isboxchecked boolean private backing field _isboxchecked both textboxes , and checkbox bound to. when run program, textboxes have bound visibility property automatically collapsed, regardless of value initialize property to. also, checkboxes not show checked if initialize property true in constructor. here code.
<stackpanel grid.column="0" margin="10,37" horizontalalignment="right" verticalalignment="top"> <textblock text="always visible" margin="5"/> <textblock text="also visible" margin="5"/> <textblock text="needs hide , unhide" margin="5" visibility="{binding isboxchecked, converter={staticresource booltovis}}"/> <checkbox content="text" ischecked="{binding isboxchecked, mode=twoway}" margin="5"/> </stackpanel>
here viewmodel.
public class newspringviewmodel implements inotifypropertychanged private _newwindow newview private _isboxchecked boolean public sub new() _newwindow = new newview _newwindow.datacontext = me _newwindow.show() isboxchecked = true end sub public property isboxchecked boolean return _isboxchecked end set(value boolean) _isboxchecked = value onpropertychanged("isboxchecked") end set end property private sub onpropertychanged(propertyname string) raiseevent propertychanged(me, new propertychangedeventargs("propertyname")) end sub public event propertychanged propertychangedeventhandler implements inotifypropertychanged.propertychanged end class
the checkbox binding property works fine running. if set break point on setter isbox checked property, debugger interrupts if check or uncheck box.
thanks help.
jon
not sure vb syntax, should not
raiseevent propertychanged(me, new propertychangedeventargs("propertyname"))
be changed to
raiseevent propertychanged(me, new propertychangedeventargs(propertyname))
that explains why there no notifications. pass parameter instead of constant string
also can directly bind textblock visibility ischecked property (reference checkbox elementname)
<stackpanel grid.column="0" margin="10,37" horizontalalignment="right" verticalalignment="top"> <textblock text="always visible" margin="5"/> <textblock text="also visible" margin="5"/> <textblock text="needs hide , unhide" margin="5" visibility="{binding ischecked, elementname=chk, converter={staticresource booltovis}}"/> <checkbox content="text" name="chk" margin="5"/> </stackpanel>