XAML 中的布尔命令参数

我有这样的代码(运行正常) :

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

其中“ s”当然是 System 命名空间。

但是这个命令被调用了很多次,它实际上膨胀了相当简单的 XAML 代码。这真的是 XAML 中布尔命令参数的最短表示法吗(除了将命令分解为几个命令之外) ?

64519 次浏览

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; }
}
}

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"

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!

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.