0

I have read numerous tutorials and spent hours trying to figure out how to bind a group of radio buttons to an enum the correct way. I have followed numerous tutorials and tried different methods with no luck.

My exact issue is: Whenever I select a radio button, it will be selected. Then, when I select another radio button, BOTH buttons will remain selected, rather than being mutual exclusive and only the second one being selected. Additionally, I think the enum in my ViewModel always ends up remaining set to the first selection and does not get correctly updated.

Here are my enum and properties:

public enum RadioButtonSelectionData
{
    SOC, iBatt, rBatt, Raw_Fifo, 
    Fifo_Length, Accumulator_Count, 
    OCV, Battery_Temperature, Slope
};

private RadioButtonSelectionData _Selection;

public RadioButtonSelectionData Selection
{
    get
    {
        return _Selection;
    }
    set
    {
        _Selection = value;
        RaisePropertyChanged("_Selection");
    }
}

Here are my radio buttons in .xaml:

<RadioButton IsChecked="{Binding Path=Selection, Converter={StaticResource enumBooleanConverter}, ConverterParameter=SOC}" Margin="30, 0, 0, 2" GroupName="Axis1" Content="SOC"/>
<RadioButton IsChecked="{Binding Path=Selection, Converter={StaticResource enumBooleanConverter}, ConverterParameter=iBatt}" Margin="30, 0, 0, 2" GroupName="Axis1" Content="iBatt"/>
<RadioButton IsChecked="{Binding Path=Selection, Converter={StaticResource enumBooleanConverter}, ConverterParameter=rBatt}" Margin="30, 0, 0, 2" GroupName="Axis1" Content="rBatt"/>
<RadioButton IsChecked="{Binding Path=Selection, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Raw_Fifo}" Margin="30, 0, 0, 2" GroupName="Axis1" Content="Raw Fifo"/>
<RadioButton IsChecked="{Binding Path=Selection, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Fifo_Length}" Margin="30, 0, 0, 2" GroupName="Axis1" Content="Fifo Length"/>
<RadioButton IsChecked="{Binding Path=Selection, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Accumulator_Count}" Margin="30, 0, 0, 2" GroupName="Axis1" Content="Accumulator Count"/>
<RadioButton IsChecked="{Binding Path=Selection, Converter={StaticResource enumBooleanConverter}, ConverterParameter=OCV}" Margin="30, 0, 0, 2" GroupName="Axis1" Content="OCV"/>
<RadioButton IsChecked="{Binding Path=Selection, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Battery_Temperature}" Margin="30, 0, 0, 2" GroupName="Axis1" Content="Battery Temperture"/>
<RadioButton IsChecked="{Binding Path=Selection, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Slope}" Margin="30, 0, 0, 2" GroupName="Axis1" Content="Slope"/>

And here is my EnumBooleanConverter class:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    string parameterString = parameter as string;
    if (parameterString == null)
        return DependencyProperty.UnsetValue;

    if (Enum.IsDefined(value.GetType(), value) == false)
        return DependencyProperty.UnsetValue;

    object parameterValue = Enum.Parse(value.GetType(), parameterString);

    return parameterValue.Equals(value);
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    string parameterString = parameter as string;
    if (parameterString == null)
        return DependencyProperty.UnsetValue;

    var s = Enum.Parse(targetType, parameterString);

    return s;
}

}

Question: Does anyone see/know where I am going wrong?? I can't figure it out after a lot of research. Thanks!

Rich E
  • 233
  • 2
  • 10
  • i have answered the similar question here http://stackoverflow.com/questions/23823844/radiobutton-binding-converters/23824451#23824451 – Nitin Jun 05 '14 at 17:39
  • I've read your post and have some questions. So if I bind each of the radio buttons to some `ICommand radio_button_pressed`, I need to send a command paramter as well to determine which radio button was pressed. The concept I get. But the issue is that my `ICommand` is defined as `public ICommand radio_button_pressed { get { return new DelegateCommand(doRadioButton); } }`. then my `doRadioButton` is defined as `private void doRadioButton()` so that does not take a parameter. So my question is how do I actually access that parameter that I would be sending from the xaml? Thanks~ – Rich E Jun 05 '14 at 18:29
  • you should create your Delegate command class as shown in this tutorial. It accepts parameters http://wpftutorial.net/DelegateCommand.html – Nitin Jun 06 '14 at 02:34

0 Answers0