3

Problem: Binding a property of type Enum to RadioButtons, using parameterized Converter. No exception thrown, Radiobutton might have validation problems (not sure). Red box around RadioButtons is shown when testing.

Info: Was trying to use the solution given in How to bind RadioButtons to an enum?

I've got an Enum like 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 my 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 any mistakes. (Maybe just saw too much lines of code today...)

Thanks for helping!!

Community
  • 1
  • 1
Daniel
  • 95
  • 2
  • 9

2 Answers2

2

Problem solved.

I found a little piece of older code inside my viewmodel where i tried to define my enum as an internal class.

So, basically, my programm was confused which enum to use - the internal class inside the viewmodel or the external class inside the models folder.

After i removed the internal enum, everything works fine.

Daniel
  • 95
  • 2
  • 9
1

First you need to check in your converter that 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 that also in ConvertBack method.

Second, write your xaml something like 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>
israel altar
  • 1,768
  • 1
  • 16
  • 24
  • Good morning, still having that problem. Added the `value==null` check. While testing, never saw that it becomes 'true'. Maybe because the default-value of an Enum is the value at index[0] of that enum, in my case the default value would be 'Training'. Actually the `Stackpanel.Resources`part is already existing, forgot to put in the code of my question. I will edit my question and add that missing part. Thanks for your help! – Daniel Jul 28 '15 at 04:33
  • You mean `CrmSystemType`? That is a Enum-Type created by myself for choosing to which of our Crm-Server i want to connect. Live- or Training-Server. – Daniel Jul 29 '15 at 03:23
  • That's the name of my Property. `private CrmSystemType systemType` `public CrmSystemType SystemType { get { return systemType; } set { systemType = value; OnPropertyChanged("SystemType"); } }` – Daniel Jul 29 '15 at 06:38
  • 1
    Ok, in a seperate project for testing explicitly this function it is working very well. Like in the solution i mentioned under "Info". So there must be some problems inside of my code. Well, now i have to digg inside of my code for it xD But thanks for helping. – Daniel Jul 29 '15 at 07:03