WPF处理FlowDocument内的链接 [英] WPF Handle links inside FlowDocument

查看:149
本文介绍了WPF处理FlowDocument内的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WPF构建简单的字典应用程序。



我使用MVVM模式,数据绑定和FlowDocument向用户显示单词。



UI由2控件:ListBox和FlowDocumentScrollViewer。



字典数据来自XML文件,此字符串的格式可能如下

 < b> Word< / b> - 一些描述。另一个< i< i>描述。参考< ref id =123>相关字< / ref> 

HTML和参考元素的顺序未定义。



我解析HTML字符串,使其成为XAML,然后从XAML创建FlowDocument对象并将其绑定到FlowDocumentScrollViewer控件的Document属性。



出现问题当我需要链接参考。元素。 (我的要求是当用户点击参考链接,引用的单词在ListBox控件中选择并显示在FlowDocumentScrollViewer中)



我的问题是有一种方法来动态创建超链接



是否有可能解决这个问题?

解决方案

有超级链接文本元素具有Command属性和Click事件。它的行为非常像一个按钮,但它在FlowDocuments内部使用。您可以使用任何一种方法来实现您以后的功能,但我倾向于使用Commands。特别是如果你使用MVVM模式来实现这一点,就像你标记的那样...


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?

解决方案

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

这篇关于WPF处理FlowDocument内的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆