XAML 中的布尔命令参数 [英] Boolean CommandParameter in XAML

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

问题描述

我有这个代码(效果很好):

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

推荐答案

这可能有点小技巧,但您可以从 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 中的布尔命令参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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