WPF Handle links inside FlowDocument

天大地大妈咪最大 提交于 2019-12-24 08:47:02

问题


I'm building simple dictionary application using WPF.

I'm using MVVM pattern, databinding and FlowDocument to display words to user.

UI consists of 2 controls: ListBox and FlowDocumentScrollViewer.

Dictionary data comes from XML file, format of this string may look like this

<b>Word</b> - Some description. Another <i>description</i>. Reference <ref id="123">related word</ref>

The order of HTML and reference elements is not defined.

I parse HTML string, make it into XAML, then from XAML I create FlowDocument object and bind it to Document property of FlowDocumentScrollViewer control.

The problem arises when I need to link ref. elements. (my requirement is when user clicks on reference link, referenced word is selected in ListBox control and shown in FlowDocumentScrollViewer)

My question is there a way to dynamically create "hyperlink"-style controls (with event or commands attached) that would take user to referenced word in dictionary.

Is it possible to solve this problem at all?


回答1:


There is the Hyperlink text element that has a Command property and a Click event. It behaves pretty much like a button, but it is used inside the FlowDocuments. You can use either method to achieve what you are after, but I tend to prefer Commands. Especially if you are implementing this using the MVVM pattern as you have tagged...



来源:https://stackoverflow.com/questions/710667/wpf-handle-links-inside-flowdocument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!