XAML 中的布尔命令参数

发布于 2024-10-17 09:14:00 字数 400 浏览 3 评论 0原文

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

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

其中“s”当然是系统名称空间。

但是这个命令被调用了很多次,它确实增加了原本相当简单的 XAML 代码。这真的是 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>

Where "s" is of course the System namespace.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梨涡 2024-10-24 09:14:00

这可能有点麻烦,但您可以从 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}"/>

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}"/>
我家小可爱 2024-10-24 09:14:00

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

<System:Boolean x:Key="FalseValue">False</System:Boolean>
<System:Boolean x:Key="TrueValue">True</System:Boolean>

并使用它,如下所示:

<Button CommandParameter="{StaticResource FalseValue}"/>

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}"/>
拿命拼未来 2024-10-24 09:14:00

或者,也许是:

<Button.CommandParameter>
    <s:Boolean>True</s:Boolean>
</Button.CommandParameter>

其中 s 是名称空间:

 xmlns:s="clr-namespace:System;assembly=mscorlib"

Or, maybe that:

<Button.CommandParameter>
    <s:Boolean>True</s:Boolean>
</Button.CommandParameter>

Where s is the namespace:

 xmlns:s="clr-namespace:System;assembly=mscorlib"
不疑不惑不回忆 2024-10-24 09:14:00

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

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

您甚至可以获得选项 TrueFalse 输入 Bool= 并输入 safety 之后!

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!

终陌 2024-10-24 09:14:00

也许类似于

<KeyBinding Key="Enter" Command="{Binding ReturnResultCommand}"
    CommandParameter="{x:Static StaticBoolean.True}" />

StaticBoolean 所在的位置

public static class StaticBoolean
{
    public static bool True
    {
        get { return true; }
    }
}

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; }
    }
}
过期情话 2024-10-24 09:14:00

这是另一种方法,您可以定义自己的标记扩展,返回 TrueFalse (或您希望的任何其他值)。然后,您只需在 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许多我不想存储在我的资源中的常见事物的类。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文