Boolean CommandParameter in XAML
Solution 1
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}"/>
Solution 2
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}"/>
Solution 3
Or, maybe that:
<Button.CommandParameter>
<s:Boolean>True</s:Boolean>
</Button.CommandParameter>
Where s is the namespace:
xmlns:s="clr-namespace:System;assembly=mscorlib"
Solution 4
I just found an even more generic solution with this markup extension:
public class SystemTypeExtension : MarkupExtension
{
private object parameter;
public int Int{set { parameter = value; }}
public double Double { set { parameter = value; } }
public float Float { set { parameter = value; } }
public bool Bool { set { parameter = value; } }
// add more as needed here
public override object ProvideValue(IServiceProvider serviceProvider)
{
return parameter;
}
}
Usage ("wpf:" is the namespace where the extension lives in):
<KeyBinding Key="F8" Command="{Binding SomeCommand}" CommandParameter="{wpf:SystemType Bool=True}"/>
You even get the options True
and False
after typing Bool=
and type safety!
Solution 5
Perhaps something like
<KeyBinding Key="Enter" Command="{Binding ReturnResultCommand}"
CommandParameter="{x:Static StaticBoolean.True}" />
where StaticBoolean
is
public static class StaticBoolean
{
public static bool True
{
get { return true; }
}
}
Related videos on Youtube

Matěj Zábský
Working as C# developer at Tollnet a.s. Check out my pet project - GeoGen (programmable procedural heightmap generator). My blog LinkedIn profile GitHub profile
Updated on January 23, 2020Comments
-
Matěj Zábský almost 3 years
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)?
-
Matěj Zábský almost 12 yearsYes. The value is passed into the command as string (which is also not what I want).
-
Matěj Zábský almost 12 yearsHmm, how would I use the converter in this context?
-
Matěj Zábský almost 12 yearsIt is not only for KeyBindings, but for Buttons and such as well.
-
Bala R almost 12 yearsSorry found something simpler.
-
H.B. almost 12 yearsThen what about my second method which i just added?
-
H.B. almost 12 yearsThat was an interesting evolution i must say, saw all the steps :P Now it's down to a bool resource (which you could do in Xaml as well, like in my answer)
-
Matěj Zábský almost 12 yearsInteresting idea, that didn't occur to me. I will try it.
-
Igor about 8 years@H.B. Why does mine always return false? I can't get this to work.
-
H.B. about 8 years@Igor: I don't know, try to construct an minimal working example and post a new question if you were not able to solve the problem in the process. Unless people know what you do it's hard to fix.
-
BendEg over 7 years@H.B. Nice answer, maybe you could add this:
xmlns:s="clr-namespace:System;assembly=mscorlib"
to your answer :) -
Onur over 6 years@marbel82 as far as I know you can omit the "Extension" in the same way as "Attribute" for attributes. Try for yourself! But adding extension won't hurt of course.
-
marbel82 over 6 yearsI really like your solution. I suggest a small improvement. Instead of
get
simply initialize a constantpublic static bool True = true;
and add another constantpublic static bool False = false;
. -
marbel82 over 6 yearsOnur you're right! I didn't know that. You should write this info in the answer. Previously I tested your code, but I had a mistake somewhere else.
-
John Sully over 5 yearsThat is so slick it is sick. Thanks for your solution.
-
Wouter almost 5 yearsJust create a markupextension per type (e.g.
BooleanExtension
) and you can write e.g.CommandParameter={x:Boolean True}
similar to docs.microsoft.com/en-us/dotnet/framework/xaml-services/… -
Alex almost 5 yearsand you need to add:
xmlns:System="clr-namespace:System;assembly=mscorlib"
to the user control