c# - Windows Phone - Binding TextBox or other control to CommandParameter of Button -
i'm making first steps using commands (by implementing icommand
interface) in windows phone applications. i've run problem can't seem figure out. i'm binding control, in case it's textbox, commandparameter
property of button
:
<button x:name="btn_search" style="{staticresource buttonnopressedstyle}" borderthickness="0" ccontrols:tilteffect.istiltenabled="true" grid.column="1" height="85" margin="0,0,0,-2" commandparameter="{binding elementname=tb_search}" command="{binding searchtermcommand}"> <button.background> <imagebrush imagesource="/assets/images/searchbtn.png" /> </button.background> </button>
when application starts , viewmodel instantiated, canexecute
method gets fired twice in row.
public override bool canexecute(object parameter) { if (parameter != null) { var textbox = parameter textbox; if ((textbox.datacontext mainpageviewmodel).searchterm == null) { (textbox.datacontext mainpageviewmodel).searchterm = ""; return true; } else if (string.isnullorwhitespace(textbox.text)) return false; else if (textbox.text.any(char.isdigit)) return false; else if (textbox.text.length < 4) return false; else if (_commandexecuting) return false; else { var bindingexpression = textbox.getbindingexpression(textbox.textproperty); bindingexpression.updatesource(); return true; } } return true; }
the first time parameter null
, second time contains textbox. because of behavior have make these first 2 times, canexecute
method returns true, else button disabled.
i've read other topics may have raisecanexecutechanged()
, i'm not sure of either. question has answers regarding issue, answers don't fit needs, since solutions wpf (using commandmanager
, or imultivalueconverter
) , others don't seem work.
is there solution make sure canexecute
fires once, or what's explanation behavior?
i think textbox not initialized when command parameter bounded first time. try bind textbox viewmodel parameter (parameter must dependencyproperty or viewmodel must implement inotifypropertychanged interface) twoway mode pass parameter command parameter.
Comments
Post a Comment