Boolean CommandParameter in XAML

61,283

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; }
    }
}
Share:
61,283

Related videos on Youtube

Matěj Zábský
Author by

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, 2020

Comments

  • Matěj Zábský
    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ý
    Matěj Zábský almost 12 years
    Yes. The value is passed into the command as string (which is also not what I want).
  • Matěj Zábský
    Matěj Zábský almost 12 years
    Hmm, how would I use the converter in this context?
  • Matěj Zábský
    Matěj Zábský almost 12 years
    It is not only for KeyBindings, but for Buttons and such as well.
  • Bala R
    Bala R almost 12 years
    Sorry found something simpler.
  • H.B.
    H.B. almost 12 years
    Then what about my second method which i just added?
  • H.B.
    H.B. almost 12 years
    That 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ý
    Matěj Zábský almost 12 years
    Interesting idea, that didn't occur to me. I will try it.
  • Igor
    Igor about 8 years
    @H.B. Why does mine always return false? I can't get this to work.
  • H.B.
    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
    BendEg over 7 years
    @H.B. Nice answer, maybe you could add this: xmlns:s="clr-namespace:System;assembly=mscorlib" to your answer :)
  • Onur
    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
    marbel82 over 6 years
    I really like your solution. I suggest a small improvement. Instead of get simply initialize a constant public static bool True = true; and add another constant public static bool False = false;.
  • marbel82
    marbel82 over 6 years
    Onur 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
    John Sully over 5 years
    That is so slick it is sick. Thanks for your solution.
  • Wouter
    Wouter almost 5 years
    Just 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
    Alex almost 5 years
    and you need to add: xmlns:System="clr-namespace:System;assembly=mscorlib" to the user control