如何将 Richtextbox 中的 WPF 超链接绑定到命令?
使用MVVM模式,如何进行动态绑定 一个 ICommand 到 a 内的超链接的单击事件 富文本框?
With the MVVM pattern, how does one go about dynamically binding
an ICommand to the click event of a hyperlink inside of a
RichTextBox?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然需要几步才能到达那里,但你可以做到。
您必须使用可绑定的富文本框,而不是 WPF 附带的富文本框,因为它不是您可以绑定的。 详细信息请参见:http://michaelsync.net /2009/06/09/bindable-wpf-richtext-editor-with-xamlhtml-convertor
一旦你有了它,你将拥有一个富文本编辑器,你可以将其绑定到 ViewModel 中的 FlowDocument .
创建 FlowDocument 后,在 ViewModel 中连接 Hyperlink.ClickEvent 的处理程序:
以下是添加处理程序的调用到 FlowDoc
这是我见过的唯一的事情。 FlowDocuments 有点奇怪,因为它们是一种数据类型和视觉元素,因此从某种意义上来说,将它驻留在 ViewModel 中感觉是错误的,但这是正确的方法。
It's a few steps to get there, but you can do it.
You have to use a bindable rich text box, rather than the one that comes with WPF which is not something you can bind. Details here: http://michaelsync.net/2009/06/09/bindable-wpf-richtext-editor-with-xamlhtml-convertor
Once you have that, you'll have a Rich Text Editor that you can bind to a FlowDocument in your ViewModel.
When your FlowDocument is created, hookup a handler for the Hyperlink.ClickEvent in your ViewModel:
Here's the call that adds the handler to the FlowDoc
This is the only thing I've ever seen done. FlowDocuments are a little strange because they are sort of a data type and sort of a visual element so in some sense it feels wrong to have it reside in your ViewModel, but this is the way to go.
您会发现很多情况下无法使用 wpf 数据绑定。 在这些场景中,您可以创建一个新控件(例如,从 RichTextBox 继承)并提供缺少的依赖属性,以便您可以使用数据绑定。
然而,创建一个新的控件来处理简单的场景效率很低。 不禁止在视图的代码隐藏文件中实现代码,这通常比创建新控件更有意义。
项目的 ViewModel 示例中显示了如何完成此操作的具体示例:
WPF 应用程序框架 (WAF)
http:// waf.codeplex.com
You find a lot scenarios where it is not possible to use wpf data binding. In these scenarios you can create a new control (e.g. inherit from RichTextBox) and provide the missing dependency properties so you can use data binding.
However, creating a new control to handle simple scenarios is inefficient. It's not forbidden to implement code in the View's code behind file and this makes often more sense than creating a new control.
A concrete example how this can be done is shown in the ViewModel sample of the project:
WPF Application Framework (WAF)
http://waf.codeplex.com