86

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)?

0

6 Answers 6

127

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}"/>
6
  • It is not only for KeyBindings, but for Buttons and such as well. Feb 14, 2011 at 22:13
  • Then what about my second method which i just added?
    – brunnerh
    Feb 14, 2011 at 22:15
  • 1
    Interesting idea, that didn't occur to me. I will try it. Feb 15, 2011 at 15:38
  • @H.B. Why does mine always return false? I can't get this to work.
    – Igor
    Oct 7, 2014 at 20:50
  • 3
    @H.B. Nice answer, maybe you could add this: xmlns:s="clr-namespace:System;assembly=mscorlib" to your answer :)
    – BendEg
    Aug 21, 2015 at 8:05
75

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}"/>
1
  • 9
    and you need to add: xmlns:System="clr-namespace:System;assembly=mscorlib" to the user control
    – Alex
    Mar 15, 2018 at 15:08
37

Or, maybe that:

<Button.CommandParameter>
    <s:Boolean>True</s:Boolean>
</Button.CommandParameter>

Where s is the namespace:

 xmlns:s="clr-namespace:System;assembly=mscorlib"
29

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!

4
  • @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.
    – Onur
    Jul 3, 2016 at 7:43
  • 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.
    – marbel82
    Jul 12, 2016 at 7:55
  • 1
    That is so slick it is sick. Thanks for your solution.
    – John Sully
    Jul 14, 2017 at 18:10
  • 2
    Just create a markupextension per type (e.g. BooleanExtension) and you can write e.g. CommandParameter={x:Boolean True} similar to learn.microsoft.com/en-us/dotnet/framework/xaml-services/…
    – Wouter
    Jan 14, 2018 at 11:26
7

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; }
    }
}
5
  • Yes. The value is passed into the command as string (which is also not what I want). Feb 14, 2011 at 21:44
  • Hmm, how would I use the converter in this context? Feb 14, 2011 at 22:12
  • Sorry found something simpler.
    – Bala R
    Feb 14, 2011 at 22:14
  • 1
    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)
    – brunnerh
    Feb 14, 2011 at 22:17
  • 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
    Jul 6, 2016 at 7:49
4

Here's another approach where you define your own markup extensions that return True or False (or any other value you wish). Then you simply use them right in XAML like any other markup extension:

public class TrueExtension : MarkupExtension {
    public override object ProvideValue(IServiceProvider serviceProvider) => true;
}

public class FalseExtension : MarkupExtension {
    public override object ProvideValue(IServiceProvider serviceProvider) => false;
}

public class DoubleExtension : MarkupExtension {
    public DoubleExtension(){};
    public DoubleExtension(double value) => Value = value;
    public double Value { get; set; }
    public override object ProvideValue(IServiceProvider serviceProvider) => Value;
}

You then use them like this (assuming your imported namespace is mx):

<KeyBinding Key="Enter"
    Command="{Binding ReturnResultCommand}"
    CommandParameter="{mx:True}" />

<Button Visibility="{Binding SomeProperty,
    Converter={SomeBoolConverter},
    ConverterParameter={mx:True}}">

<!-- This guarantees the value passed is a double equal to 42.5 -->
<Button Visibility="{Binding SomeProperty,
    Converter={SomeDoubleConverter},
    ConverterParameter={mx:Double 42.5}}">

I actually define lots of custom MarkupExtension classes for a lot of common things that I don't want to necessarily store in my resources.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.