WPF Handle links inside FlowDocument

data-bindingflowdocumentmvvmwpf

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?

Best Answer

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...

Related Topic