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

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

问题描述

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



Click事件超链接似乎不会触发,除非 Ctrl 被按住,所以我不知道如何继续。

解决方案



加载到我的RichTextBox中的内容只是存储(或输入)为一个简单的字符串。我有子类化RichTextBox允许绑定到它的Document属性。



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

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

超链接textLink = new超链接(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文档中的链接。


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.

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.

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.

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;

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.

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

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