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!