3

ViewModelのブールプロパティにバインドしている2つのRadioButtonがあります。残念ながら、'targetType'パラメーターがnullであるため、コンバーターでエラーが発生します。

これで、targetTypeパラメーターがnullになることを期待していませんでした(TrueまたはFalseを期待していました)。ただし、RadioButtonのIsCheckedプロパティがnull許容のブール値であることに気付いたので、この種の説明があります。

XAMLで何かを修正できますか、それともソリ​​ューションの既存のコンバーターを変更する必要がありますか?

これが私のXAMLです。

<RadioButton Name="UseTemplateRadioButton" Content="Use Template" 
                GroupName="Template"
                IsChecked="{Binding UseTemplate, Mode=TwoWay}" />
<RadioButton Name="CreatNewRadioButton" Content="Create New"
                GroupName="Template"
                IsChecked="{Binding Path=UseTemplate, Mode=TwoWay, Converter={StaticResource InverseBooleanConverter}}"/>

これは、ソリューション全体で使用している既存のコンバーターです。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)));
} 
4

1 に答える 1

3

コンバーターを変更する必要があります。または、おそらくさらに良いのは、新しいコンバーターを使用することです。

[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
}
于 2013-03-18T16:37:08.403 に答える