在WPF中实现互斥的单选按钮的问题

时间:2014-06-05 17:35:00

标签: .net wpf mvvm radio-button ivalueconverter

我已阅读了大量教程,并花了数小时试图找出如何将一组单选按钮绑定到枚举的正确方法。我已经遵循了许多教程并尝试了不同的方法,没有运气。

我确切的问题是:每当我选择一个单选按钮时,它都会被选中。然后,当我选择另一个单选按钮时,BOTH按钮将保持选中状态,而不是相互排斥,只选择第二个。另外,我认为我的ViewModel中的枚举总是最终保持设置为第一个选择,并且无法正确更新。

以下是我的enum和属性:

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");
    }
}

以下是.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"/>

这是我的EnumBooleanConverter课程:

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;
}

}

问题:有没有人看到/知道我哪里出错?经过大量研究后我无法弄清楚。谢谢!

0 个答案:

没有答案