我在listview中有一个单选按钮组。这个listview的行(其中包含单选按钮grp)是一个可观察的集合。在不使用转换器的情况下将Radiobuttons绑定到它们的enumvalues
我写的代码是这样的:
XAML中:
<RadioButton Content="EnumValueName1"
GroupName="RadButGrp1"
IsChecked="{Binding propertyName,Mode=TwoWay,Converter={StaticResource EnumToBoolConverter},ConverterParameter=EnumValueName1}" >
</RadioButton>
<RadioButton Content="EnumValueName2"
GroupName="RadButGrp1"
IsChecked="{Binding propertyName,Mode=TwoWay,Converter={StaticResource EnumToBoolConverter},ConverterParameter=EnumValueName2}">
</RadioButton>
<RadioButton Content="EnumValueName3"
GroupName="RadButGrp1"
IsChecked="{Binding propertyName,Mode=TwoWay,Converter={StaticResource EnumToBoolConverter},ConverterParameter=EnumValueName3}">
</RadioButton>
我试图直接绑定到我的数据结构,称为propertyName的数据字段定义保存这些表值。这个视图的ViewModel类中没有这个字段。我这样做是为了避免跟踪我目前正在填充的集合的索引。 (或者,所以我想,认为!)
转换器:
public class EnumBooleanConverter : IValueConverter
{
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 (value == null || 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 || value.Equals(false))
return DependencyProperty.UnsetValue;
return Enum.Parse(targetType, parameterString);
}
}
的问题是,在Enum.Parse线ConvertBack功能,以下参数发生异常:
提供的类型必须是Enum。 参数名:enumType
有没有办法enum类型返回绑定?如何告诉单选按钮它代表的枚举值?我该如何编写一个函数,将适当的枚举值返回给绑定?
希望你们能帮上忙。提前致谢!
你见过这个:http://stackoverflow.com/questions/397556/how-to-bind-radiobuttons-to-an-enum – Phil 2013-03-12 14:12:53
只有一个评论,因为我没有按照完整的问题。我所做的就是将枚举打包为字典中的键值。 – Paparazzi 2013-03-12 14:18:29
我试着从你给出的链接解决方案,但我仍然得到相同的错误。 – pyridot 2013-03-12 14:35:02