c# - Binding RadioButton to Enum -


problem: binding property of type enum radiobuttons, using parameterized converter. no exception thrown, radiobutton might have validation problems (not sure). red box around radiobuttons shown when testing.

info: trying use solution given in how bind radiobuttons enum?

i've got enum this:

namespace crmverwaltungstools.models {    public enum crmsystemtype    {      training = 0,      live = 1    } } 

booleantoenumconverter:

    public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         return value.equals(parameter);     }      public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         return value.equals(true) ? (crmsystemtype)parameter : binding.donothing;     } 

and inside window:

xmlns:models="clr-namespace:crmverwaltungstool.models"       <stackpanel grid.column="0" grid.columnspan="2" grid.row="1" orientation="horizontal">           <stackpanel.resources>                 <converter:radiobuttonischeckedtocrmsystemtypeconverter x:key="rbischeckedtocrmsystemtypeconverter" />           </stackpanel.resources>            <radiobutton content="schulungs-system" groupname="rbg_selectsystem"                                  ischecked="{binding path=systemtype, converter={staticresource rbischeckedtocrmsystemtypeconverter},                                  converterparameter={x:static models:crmsystemtype.training}}"/>           <radiobutton content="live-system" groupname="rbg_selectsystem"                                  ischecked="{binding path=systemtype, converter={staticresource rbischeckedtocrmsystemtypeconverter},                                  converterparameter={x:static models:crmsystemtype.live}}"/>       </stackpanel> 

can't see mistakes. (maybe saw lines of code today...)

thanks helping!!

first need check in converter value isn't null:

public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         if (value == null)         {             return false;         }         return value.equals(parameter);     } 

do in convertback method.

second, write xaml that:

<stackpanel>     <stackpanel.resources>                   <local:enumtobooleanconverter x:key="enumtobooleanconverter" />               </stackpanel.resources>     <radiobutton ischecked="{binding path=yourenumproperty, converter={staticresource enumtobooleanconverter}, converterparameter={x:static local:yourenumtype.enum1}}" />     <radiobutton ischecked="{binding path=yourenumproperty, converter={staticresource enumtobooleanconverter}, converterparameter={x:static local:yourenumtype.enum2}}" /> </stackpanel> 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -