XAML中的布尔CommandParameter

Mat*_*ský 66 wpf xaml routed-commands

我有这个代码(工作正常):

<KeyBinding Key="Enter" Command="{Binding ReturnResultCommand}">
    <KeyBinding.CommandParameter>
        <s:Boolean>
            True
        </s:Boolean>
    </KeyBinding.CommandParameter>
</KeyBinding>
Run Code Online (Sandbox Code Playgroud)

其中"s"当然是System命名空间.

但是这个命令被调用了很多次,它实际上膨胀了相当简单的XAML代码.这是否是XAML中布尔命令参数的最短符号(除了将命令分成几个命令)?

H.B*_*.B. 94

这可能是一个黑客,但你可以从KeyBinding类派生:

public class BoolKeyBinding : KeyBinding
{
    public bool Parameter
    {
        get { return (bool)CommandParameter; }
        set { CommandParameter = value; }
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

<local:BoolKeyBinding ... Parameter="True"/>
Run Code Online (Sandbox Code Playgroud)

而另一个不那么奇怪的解决方案:

xmlns:s="clr-namespace:System;assembly=mscorlib"
Run Code Online (Sandbox Code Playgroud)
<Application.Resources>
    <!-- ... -->
    <s:Boolean x:Key="True">True</s:Boolean>
    <s:Boolean x:Key="False">False</s:Boolean>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)

用法:

<KeyBinding ... CommandParameter="{StaticResource True}"/>
Run Code Online (Sandbox Code Playgroud)

  • @HB很好的答案,也许你可以添加这个:`xmlns:s ="clr-namespace:System; assembly = mscorlib"`到你的答案:) (2认同)

小智 60

最简单的方法是在参考资料中定义以下内容

<System:Boolean x:Key="FalseValue">False</System:Boolean>
<System:Boolean x:Key="TrueValue">True</System:Boolean>
Run Code Online (Sandbox Code Playgroud)

并使用它像:

<Button CommandParameter="{StaticResource FalseValue}"/>
Run Code Online (Sandbox Code Playgroud)

  • 你需要添加:`xmlns:System ="clr-namespace:System; assembly = mscorlib"`到用户控件 (3认同)

Onu*_*nur 23

我刚刚找到了一个使用此标记扩展的更通用的解决方案:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

用法("wpf:"是扩展所在的命名空间):

<KeyBinding Key="F8" Command="{Binding SomeCommand}" CommandParameter="{wpf:SystemType Bool=True}"/>
Run Code Online (Sandbox Code Playgroud)

您甚至可以在输入后输入选项TrueFalse键入Bool=安全性!

  • 只需为每种类型创建一个标记扩展(例如`BooleanExtension`),您就可以编写类似于https://docs.microsoft.com/en-us/dotnet/framework/xaml-services的`CommandParameter={x:Boolean True}` /built-in-types-for-common-xaml-language-primitives (2认同)

小智 16

或者,也许那样:

<Button.CommandParameter>
    <s:Boolean>True</s:Boolean>
</Button.CommandParameter>
Run Code Online (Sandbox Code Playgroud)

其中s是命名空间:

 xmlns:s="clr-namespace:System;assembly=mscorlib"
Run Code Online (Sandbox Code Playgroud)


Bal*_*a R 6

也许是这样的

<KeyBinding Key="Enter" Command="{Binding ReturnResultCommand}"
    CommandParameter="{x:Static StaticBoolean.True}" />
Run Code Online (Sandbox Code Playgroud)

这里StaticBoolean

public static class StaticBoolean
{
    public static bool True
    {
        get { return true; }
    }
}
Run Code Online (Sandbox Code Playgroud)