Boolean CommandParameter in XAML
I have this code (which works just right):
<KeyBinding Key="Enter" Command="{Binding ReturnResultCommand}">
<KeyBinding.CommandParameter>
<s:Boolean>
True
</s:Boolean>
</KeyBinding.CommandParameter>
</KeyBinding>
Where "s" is of course the System namespace.
But this command is called quite a few times and it really inflates otherwise rather simple XAML code. Is this really the shortest notation of boolean command parameter in XAML (other than splitting the command into several commands)?
This might be a bit of a hack but you can derive from the KeyBinding
class:
public class BoolKeyBinding : KeyBinding
{
public bool Parameter
{
get { return (bool)CommandParameter; }
set { CommandParameter = value; }
}
}
Usage:
<local:BoolKeyBinding ... Parameter="True"/>
And another not so weird solution:
xmlns:s="clr-namespace:System;assembly=mscorlib"
<Application.Resources>
<!-- ... -->
<s:Boolean x:Key="True">True</s:Boolean>
<s:Boolean x:Key="False">False</s:Boolean>
</Application.Resources>
Usage:
<KeyBinding ... CommandParameter="{StaticResource True}"/>
The easiest is to define the following in the Resources
<System:Boolean x:Key="FalseValue">False</System:Boolean>
<System:Boolean x:Key="TrueValue">True</System:Boolean>
and use it like:
<Button CommandParameter="{StaticResource FalseValue}"/>
Or, maybe that:
<Button.CommandParameter>
<s:Boolean>True</s:Boolean>
</Button.CommandParameter>
Where s is the namespace:
xmlns:s="clr-namespace:System;assembly=mscorlib"