c# - How to check valid selection when adding data from combo boxes? -


i have 2 combo boxes , add button on view, both combo boxes must have item selected them in order there valid selection.

i tried disabling add button's enabled property if both combo boxes have no selection, button re-enabled if 1 of combo boxes has been selected.

can suggest fix situation, or point out i've gone wrong setup?

the button's enabled property bound property in viewmodel:

 //this button enable property bound combo boxes being selected  ----->         <button x:name="addgradebtn"                 grid.row="2"                 horizontalalignment="left"                 command="{binding path=addgradecommand}"                 content="add grade"                 isenabled="{binding buttonenabled,                                     mode=twoway}" />          <combobox x:name="subjectcmbbx"                   grid.row="1"                   grid.columnspan="2"                   width="199"                   horizontalalignment="left"                   verticalalignment="top"                   displaymemberpath="subject"                   header="subjects"                   itemssource="{binding subjects}"                   placeholdertext="pick subject"                   selecteditem="{binding selectedsubject,                                          mode=twoway}" />           <combobox x:name="ordinarygradecmbbx"                   grid.row="1"                   grid.column="0"                   grid.columnspan="2"                   width="170"                   horizontalalignment="right"                   displaymemberpath="key"                   header="grades"                   itemssource="{binding ordinarygradepointkv}"                   placeholdertext="pick grade"                   selectedvalue="{binding selectedordinarygrade,                                           mode=twoway}"                   visibility="{binding ishigher,                                        mode=twoway,                                        converter={staticresource booltononvisibilityconverter}}" />          <combobox x:name="highergradecmbbx"                   grid.row="1"                   grid.column="0"                   grid.columnspan="2"                   width="170"                   horizontalalignment="right"                   displaymemberpath="key"                   header="grades"                   itemssource="{binding highergradepointkv}"                   placeholdertext="pick grade"                   selectedvalue="{binding selectedhighergrade,                                           mode=twoway}"                   visibility="{binding ishigher,                                        mode=twoway,                                        converter={staticresource booltovisibilityconverter}}" /> 

and scaled down version of viewmodel:

namespace lc_points.viewmodel { public class mainviewmodel : viewmodelbase { private readonly irepository _repository = app.scoresrepository;

    public mainviewmodel()     {         //call methods initilise list data         initsubjecttypes();         initordinarygradepairs();         inithighergradepairs();     }      public list<scoremodel> subjects { get; set; }     public list<stringkeyvalue> highergradepointkv { get; set; }     public list<stringkeyvalue> ordinarygradepointkv { get; set; }      //button enabled binding set based on combo boxes being selected -->     private scoremodel _selectedsubject;     public scoremodel selectedsubject     {         { return _selectedsubject; }         set         {             if (value != _selectedsubject)             {                 _selectedsubject = value;                 raisepropertychanged("selectedsubject");                 //i set buttons enabled property true combo box has been selected,  

but want enabled if both combo boxes have been selected ---> buttonenabled = true; } else { buttonenabled = false; } } }

    private stringkeyvalue _selectedhighergrade;     public stringkeyvalue selectedhighergrade     {         { return _selectedhighergrade; }         set         {             if (value != _selectedhighergrade)             {                 _selectedhighergrade = value;                 raisepropertychanged("selectedhighergrade");                 buttonenabled = true;             }             else             {                 buttonenabled = false;             }         }     }      private stringkeyvalue _selectedordinarygrade;     public stringkeyvalue selectedordinarygrade     {         { return _selectedordinarygrade; }         set         {             if (value != _selectedordinarygrade)             {                 _selectedordinarygrade = value;                 raisepropertychanged("selectedordinarygrade");                 buttonenabled = true;              }             else             {                 buttonenabled = false;             }         }     }  } 

}

use multibinding button isenabled below,

  <button.isenabled>      <multibinding converter="{staticresource checkifbothselectedmulticonverter}">                 <binding path="combo1" />                 <binding path="combo2" />             </multibinding> </button.isenabled> 

check condition in checkifbothselectedmulticonverter convertor, if both value true return true else false.

public class isenabledcheckconverter : imultivalueconverter {       public object convert(object[ ] values, type targettype, object parameter, cultureinfo culture) {       if(convert.toboolean(values[0]) && convert.toboolean(values[1]))         {             return true;         }      return false;         }  public object convert(object[ ] values, type targettype, object parameter, cultureinfo culture) {      //convert logic } } 

this got multi binding in win rt/universal app

 <textblock fontsize="20" textwrapping="wrap" foreground="cyan">   <mb:multibindinglinker.attach>    <mb:multibindings>     <mb:multibinding targetproperty="text" converter="{staticresource concatmulticonverter}">       <mb:binding path="stringvalue" />       <mb:binding path="text" elementname="concattextbox1"/>       <mb:binding path="text" elementname="concattextbox2" converter="{staticresource touppercaseconverter}"/>       <mb:binding path="actualwidth" relativesource="{relativesource self}" />  </mb:multibinding>  </mb:multibindings>  </mb:multibindinglinker.attach> </textblock> 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -