在不按住 CTRL 的情况下单击 RichTextBox 中的超链接 - WPF [英] Clicking HyperLinks in a RichTextBox without holding down CTRL - WPF

查看:34
本文介绍了在不按住 CTRL 的情况下单击 RichTextBox 中的超链接 - WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WPF RichTextBox,其中 isReadOnly 设置为 True.我希望用户能够点击 RichTextBox 中包含的超链接,而无需按住 Ctrl.

I have a WPF RichTextBox with isReadOnly set to True. I would like users to be able to click on HyperLinks contained within the RichTextBox, without them having to hold down Ctrl.

除非按住 Ctrl,否则 HyperLink 上的 Click 事件似乎不会触发,所以我不确定如何继续.

The Click event on the HyperLink doesn't seem to fire unless Ctrl is held-down, so I'm unsure of how to proceed.

推荐答案

设法解决了这个问题,几乎是偶然的.

Managed to find a way around this, pretty much by accident.

加载到我的 RichTextBox 中的内容只是作为纯字符串存储(或输入).我已经将 RichTextBox 子类化以允许绑定它的 Document 属性.

The content that's loaded into my RichTextBox is just stored (or inputted) as a plain string. I have subclassed the RichTextBox to allow binding against it's Document property.

与问题相关的是,我有一个 IValueConverter Convert() 重载,看起来像这样(解决方案的非必要代码已被删除):

What's relevant to the question, is that I have an IValueConverter Convert() overload that looks something like this (code non-essential to the solution has been stripped out):

FlowDocument doc = new FlowDocument();
Paragraph graph = new Paragraph();

Hyperlink textLink = new Hyperlink(new Run(textSplit));
textLink.NavigateUri = new Uri(textSplit);
textLink.RequestNavigate += 
  new System.Windows.Navigation.RequestNavigateEventHandler(navHandler);

graph.Inlines.Add(textLink);
graph.Inlines.Add(new Run(nonLinkStrings));

doc.Blocks.Add(graph);

return doc;

这让我得到了我想要的行为(将纯字符串推入 RichTextBox 并获取格式),它还导致链接的行为类似于普通链接,而不是嵌入在 Word 文档中的链接.

This gets me the behavior I want (shoving plain strings into RichTextBox and getting formatting) and it also results in links that behave like a normal link, rather than one that's embedded in a Word document.

这篇关于在不按住 CTRL 的情况下单击 RichTextBox 中的超链接 - WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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