私はMVVM Lightライブラリを使用しています。このライブラリからはRelayCommand<T>
を使用して、引数がT
のコマンドを定義します。Nullable <Boolean>の値をCommandParameterに渡すにはどうすればよいですか?
は今、私はタイプNullable<bool>
の引数を必要とRelayCommand
を定義している:
private RelayCommand<bool?> _cmdSomeCommand;
public RelayCommand<bool?> CmdSomeCommand
{
get
{
if (_cmdSomeCommand == null)
{ _cmdSomeCommand = new RelayCommand<bool?>(new Action<bool?>((val) => { /* do work */ })); }
return _cmdSomeCommand;
}
}
私は私のXAMLコードからCommandParameterを割り当てることができますどのように?
私はブール値を渡すために試してみたが、それは次の例外が発生:
System.InvalidCastExceptionの: 'を可能System.Boolean' から無効なキャストをSystem.Nullable`1 [」に[System.Boolean、mscorlib、Version = 4.0.0.0、Culture =ニュートラル、PublicKeyToken = b77a5c561934e089]] '。
また、boolを含む静的プロパティを定義しようとしましたか?値およびXAMLからそれらを参照:
public static class BooleanHelper
{
public static bool True { get { return true; } }
public static bool False { get { return false; } }
public static bool? NullableTrue { get { return true; } }
public static bool? NullableFalse { get { return false; } }
}
XAML:
<Button Command="{Binding CmdSomeCommand}" CommandParameter="{x:Static my:BooleanHelper.NullableTrue}" />
しかし、これがスローされるように同じ例外が発生します。私もnew Nullable<bool>(true)
を返そうとしましたが、期待どおり、これは同じ結果をもたらします。
あなたはこれを見ましたか? http://stackoverflow.com/questions/2850629/declare-a-nullable-int-int-using-xaml – tolanj