XAML中的布尔CommandParameter

我有这个代码(工作正常):
<KeyBinding Key="Enter" Command="{Binding ReturnResultCommand}">
    <KeyBinding.CommandParameter>
        <s:Boolean>
            True
        </s:Boolean>
    </KeyBinding.CommandParameter>
</KeyBinding>
其中“s”当然是System命名空间。 但是这个命令被调用了很多次,它实际上膨胀了相当简单的XAML代码。这是否是XAML中布尔命令参数的最短符号(除了将命令分成几个命令)?     
已邀请:
这可能是一个黑客,但你可以从
KeyBinding
类派生:
public class BoolKeyBinding : KeyBinding
{
    public bool Parameter
    {
        get { return (bool)CommandParameter; }
        set { CommandParameter = value; }
    }
}
用法:
<local:BoolKeyBinding ... Parameter="True"/>
而另一个不那么奇怪的解决方案:
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>
用法:
<KeyBinding ... CommandParameter="{StaticResource True}"/>
    
最简单的方法是在参考资料中定义以下内容
<System:Boolean x:Key="FalseValue">False</System:Boolean>
<System:Boolean x:Key="TrueValue">True</System:Boolean>
并使用它像:
<Button CommandParameter="{StaticResource FalseValue}"/>
    
我刚刚找到了一个更通用的解决方案,带有这个标记扩展:
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;
    }
}
用法(“wpf:”是扩展所在的命名空间):
<KeyBinding Key="F8" Command="{Binding SomeCommand}" CommandParameter="{wpf:SystemType Bool=True}"/>
键入
Bool=
后,您甚至可以选择
True
False
并键入安全!     
或者,也许那样:
<Button.CommandParameter>
    <s:Boolean>True</s:Boolean>
</Button.CommandParameter>
其中s是命名空间:
 xmlns:s="clr-namespace:System;assembly=mscorlib"
    
也许是这样的
<KeyBinding Key="Enter" Command="{Binding ReturnResultCommand}"
    CommandParameter="{x:Static StaticBoolean.True}" />
StaticBoolean
在哪里
public static class StaticBoolean
{
    public static bool True
    {
        get { return true; }
    }
}
    
这是另一种方法,您可以定义自己的标记扩展,返回
True
False
(或您希望的任何其他值)。然后你就像在任何其他标记扩展中一样在XAML中使用它们:
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;
}
然后你就像这样使用它们(假设导入的命名空间是
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}}">
我实际上为很多常见的东西定义了很多自定义的
MarkupExtension
类,我不希望将它们存储在我的资源中。     

要回复问题请先登录注册