WPF RadioButton InverseBooleanConverter不工作

我有两个RadioButtons,我绑定到ViewModel中的布尔属性。 不幸的是我在转换器中收到错误,因为’targetType’参数为null。

现在我并不期望targetType参数通过为null(我期待True或False)。 但是我注意到RadioButton的IsChecked属性是一个可以为空的bool,所以这种解释。

我可以更正XAML中的某些内容,还是应该更改解决方案的现有转换器?

这是我的XAML:

  

这是我使用解决方案范围的InverseBooleanConverter的现有转换器:

 public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if ((targetType != typeof(bool)) && (targetType != typeof(object))) { throw new InvalidOperationException("The target must be a boolean"); } return !(((value != null) && ((IConvertible)value).ToBoolean(provider))); } 

你需要更换转换器,或者更好的是,使用新的转换器。

 [ValueConversion(typeof(bool?), typeof(bool))] public class Converter : IValueConverter { #region IValueConverter Members public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (targetType != typeof(bool?)) { throw new InvalidOperationException("The target must be a nullable boolean"); } bool? b = (bool?)value; return b.HasValue && b.Value; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return value; } #endregion }