为 TextBlock 启用命令绑定
我正在开发一个 WPF 应用程序,并且有一个 TextBlock,我想使用命令绑定在单击时触发命令。实现这一目标的最佳方法是什么?
TextBlock 控件没有 Command 属性,但有 CommandManager。这是什么?它可以用于命令绑定吗?我见过许多其他控件也具有此属性。
是否有一些我监督过的可以使用的控件?例如,是否建议使用按钮并将其设计为看起来不像按钮?
是否有一些控件支持命令绑定,我可以将它们包裹在 TextBlock 上?
我是否应该创建一个自定义控件,它基本上是一个 TextBlock,但具有额外的属性 Command 和 CommandArgument,可以在例如 MouseLeftButtonDown 属性上启用命令绑定。
I am developing a WPF application, and have a TextBlock which I want to use command binding to trigger a command on when clicked. What's the best way to achieve this?
The TextBlock-control does not have a Command property, but it does have a CommandManager. What is this? Can it be used for command bindings? I've seen many other controls as well with this property..
Is there some control I have overseen that can be used? Is it e.g. recommended to use a button and style it to not look like a button?
Is there some controls supporting Command bindings which I can wrap around the TextBlock?
Should I create a custom control which basically is a TextBlock, but with extra properties Command and CommandArgument which enables command binding on e.g. the MouseLeftButtonDown property.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的。最简单的方法是重新模板化按钮,使其像 TextBlock 一样工作,并利用按钮类上的命令属性。
像这样的东西:
Yes. The simplest approach would be to re-template a button to act like a TextBlock and leverage the command property on the button class.
Something like this:
下面的 XAML 可用于将命令绑定添加到 WPF TextBlock,然后该文本块将作用于鼠标操作。
Command
可以是内置应用程序命令之一,它将使用如上所示的语法,也可以是继承自的自定义
Command
ICommand 接口。在这种情况下,语法将是:MouseAction
不提供任何有关放置内容的 Intellisense 提示(在 VS2015 中),因此您必须进行一些挖掘才能获得有效的枚举。从 .NET 4.5 开始,
MouseAction
的有效条目是:上面显示的常量取自 MSDN。
The below XAML can be used to add a Command Binding to a WPF TextBlock which will then work on a mouse action.
The
Command
can be one of the built-in application commands, which would use the syntax as shown above, or it can be a customCommand
that inherits from theICommand
Interface. In that case, the syntax would be:The
MouseAction
doesn't provide any Intellisense tips on what to put there (in VS2015), so you have to do a little digging to get the valid enumerations.As of .NET 4.5, the valid entries for
MouseAction
are:Constants shown above are taken from MSDN.
您是否看过 http://www.java2s.com/Tutorial/ CSharp/0470__Windows-Presentation-Foundation/BindTextBoxsavecommandtoCommandBinding.htm
Have you seen http://www.java2s.com/Tutorial/CSharp/0470__Windows-Presentation-Foundation/BindTextBoxsavecommandtoCommandBinding.htm