Boolean CommandParameter in XAML
Asked Answered
G

6

87

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

Gezira answered 14/2, 2011 at 21:24 Comment(0)
R
128

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}"/>
Rudyrudyard answered 14/2, 2011 at 21:48 Comment(6)
It is not only for KeyBindings, but for Buttons and such as well.Unorganized
Then what about my second method which i just added?Rudyrudyard
Interesting idea, that didn't occur to me. I will try it.Unorganized
@H.B. Why does mine always return false? I can't get this to work.Wendel
@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.Rudyrudyard
@H.B. Nice answer, maybe you could add this: xmlns:s="clr-namespace:System;assembly=mscorlib" to your answer :)Beccafico
T
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}"/>
Tore answered 9/11, 2012 at 17:47 Comment(1)
and you need to add: xmlns:System="clr-namespace:System;assembly=mscorlib" to the user controlPropinquity
J
38

Or, maybe that:

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

Where s is the namespace:

 xmlns:s="clr-namespace:System;assembly=mscorlib"
Joslin answered 29/8, 2014 at 10:53 Comment(0)
L
30

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!

Lump answered 13/8, 2015 at 16:31 Comment(4)
@Delouse 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.Lump
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.Delouse
That is so slick it is sick. Thanks for your solution.Botticelli
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/…Pericline
H
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; }
    }
}
Hedvah answered 14/2, 2011 at 21:40 Comment(5)
Yes. The value is passed into the command as string (which is also not what I want).Unorganized
Hmm, how would I use the converter in this context?Unorganized
Sorry found something simpler.Hedvah
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)Rudyrudyard
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;.Delouse
I
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.

Incapacity answered 4/1, 2019 at 5:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.