XAML中的布尔CommandParameter [英] Boolean CommandParameter in XAML

查看:407
本文介绍了XAML中的布尔CommandParameter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码(可以正常使用):

I have this code (which works just right):

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

"s"当然是系统名称空间.

Where "s" is of course the System namespace.

但是此命令被调用了好几次,并且实际上会使原本相当简单的XAML代码膨胀.这真的是XAML中布尔命令参数的最短表示法(​​除了将命令分成几个命令之外)?

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

推荐答案

这可能有点hack,但是您可以从KeyBinding类派生

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

用法:

<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}"/>

这篇关于XAML中的布尔CommandParameter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆