wpf XAML 中的布尔命令参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4997446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Boolean CommandParameter in XAML
提问by Matěj Zábsky
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.
其中“s”当然是系统命名空间。
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)?
但是这个命令被调用了很多次,它确实膨胀了其他相当简单的 XAML 代码。这真的是 XAML 中布尔命令参数的最短表示法(除了将命令拆分为多个命令)?
回答by H.B.
This might be a bit of a hack but you can derive from the KeyBinding
class:
这可能有点小技巧,但您可以从KeyBinding
该类派生:
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}"/>
回答by Gordon
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}"/>
回答by Onur
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):
用法(“wpf:”是扩展所在的命名空间):
<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!
您还可以得到的选项True
,并False
键入之后Bool=
和类型安全!
回答by javicabanas
Or, maybe that:
或者,也许是:
<Button.CommandParameter>
<s:Boolean>True</s:Boolean>
</Button.CommandParameter>
Where s is the namespace:
其中 s 是命名空间:
xmlns:s="clr-namespace:System;assembly=mscorlib"
回答by Bala R
Perhaps something like
也许像
<KeyBinding Key="Enter" Command="{Binding ReturnResultCommand}"
CommandParameter="{x:Static StaticBoolean.True}" />
where StaticBoolean
is
这里StaticBoolean
是
public static class StaticBoolean
{
public static bool True
{
get { return true; }
}
}
回答by Mark A. Donohoe
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:
这是另一种方法,您可以定义自己的标记扩展,返回True
or 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;
}
You then use them like this (assuming your imported namespace is mx
):
然后您可以像这样使用它们(假设您导入的命名空间是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.
我实际上MarkupExtension
为很多我不想存储在我的资源中的常见事物定义了很多自定义类。