XAML 中的布尔命令参数
我有这段代码(效果很好):
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这可能有点麻烦,但您可以从
KeyBinding
类派生:用法:
另一个不太奇怪的解决方案:
用法:
This might be a bit of a hack but you can derive from the
KeyBinding
class:Usage:
And another not so weird solution:
Usage:
最简单的方法是在资源中定义以下内容
并使用它,如下所示:
The easiest is to define the following in the Resources
and use it like:
或者,也许是:
其中 s 是名称空间:
Or, maybe that:
Where s is the namespace:
我刚刚找到了一个带有此标记扩展的更通用的解决方案:
用法(“wpf:”是扩展所在的命名空间):
您甚至可以获得选项
True
和False
输入Bool=
并输入 safety 之后!I just found an even more generic solution with this markup extension:
Usage ("wpf:" is the namespace where the extension lives in):
You even get the options
True
andFalse
after typingBool=
and type safety!也许类似于
StaticBoolean
所在的位置Perhaps something like
where
StaticBoolean
is这是另一种方法,您可以定义自己的标记扩展,返回
True
或False
(或您希望的任何其他值)。然后,您只需在 XAML 中使用它们,就像任何其他标记扩展一样:然后像这样使用它们(假设您导入的命名空间是
mx
):我实际上定义了很多自定义
MarkupExtension
许多我不想存储在我的资源中的常见事物的类。Here's another approach where you define your own markup extensions that return
True
orFalse
(or any other value you wish). Then you simply use them right in XAML like any other markup extension:You then use them like this (assuming your imported namespace is
mx
):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.