c# - Controlling a buttons enabled state using 3 combobox selecteditem values -


i developing wpf application. have 3 comboboxes. each combobox has list source , there 3 items : 1,2,3 in each combobox. have button too. want disable button if user has selected same value in atleast 2 of comboboxes. ie. if user selects 1 in first cb , 1 in second cb too, disable button. have tried achieve using below code within of button. doesn’t work anyway.

<button> .... <style>      <style.triggers>         <multidatatrigger>             <multidatatrigger.conditions>                 <condition binding="{binding elementname=cb1, path=selecteditem}" value="1" />                 <condition binding="{binding elementname=cb2, path=selecteditem}" value="1" />                 <condition binding="{binding elementname=cb3, path=selecteditem}" value="1" />             </multidatatrigger.conditions>             <setter property="isenabled" value="false" />         </multidatatrigger>     </style.triggers> </style> </button> 

could please suggest better solution same in xaml itself?

i use multivalueconverter achieve this:

public class isdistinctconverter : imultivalueconverter {     public object convert(object[] values, type targettype, object parameter, cultureinfo culture)     {         return values.distinct().count() == values.length;     }      public object[] convertback(object value, type[] targettypes, object parameter, cultureinfo culture)     {         throw new notsupportedexception();     } } 

the convert method returns true if items in passed list unique. in xaml can use converter this:

<button content="my button">     <button.resources>         <!-- can move resource place without problem -->         <local:isdistinctconverter x:key="isdistinctconverter"/>     </button.resources>     <button.isenabled>         <multibinding converter="{staticresource isdistinctconverter}">             <binding path="selecteditem" elementname="cb1"/>             <binding path="selecteditem" elementname="cb2"/>             <binding path="selecteditem" elementname="cb3"/>         </multibinding>     </button.isenabled> </button> 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -