在不按住 CTRL 的情况下单击 RichTextBox 中的超链接 - WPF
我有一个 WPF RichTextBox,其 isReadOnly
设置为 True
。 我希望用户能够单击 RichTextBox 中包含的超链接,而无需按住 Ctrl。
除非按住 Ctrl,否则超链接上的 Click 事件似乎不会触发,因此我不确定如何继续。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不要显式处理任何鼠标事件,也不要显式强制光标 - 就像每个答案中所建议的那样。
也不需要将完整的 RichTextBox 设为只读(如另一个答案中所建议的)。
要使
Hyperlink
在不按Ctrl键的情况下可点击,必须将Hyperlink
设置为只读,例如,将其包装到TextBlock
中(当然,也可以将完整的RichTextBox
设为只读)。然后只需处理
Hyperlink.RequestNavigate
事件或/并将ICommand
附加到Hyperlink.Command
属性:Do not handle any mouse events explicitly and do not force the cursor explicitly - like suggested in every answer.
It's also not required to make the complete
RichTextBox
read-only (as suggested in another answer).To make the
Hyperlink
clickable without pressing the Ctrl key, theHyperlink
must be made read-only e.g., by wrapping it into aTextBlock
(or alternatively by making the completeRichTextBox
read-only, of course).Then simply handle the
Hyperlink.RequestNavigate
event or/and attach anICommand
to theHyperlink.Command
property:如果您想将箭头变成手形光标,并且始终没有默认系统导航,请使用以下方法。
If you want to turn Arrow into a Hand cursor always without default system navigation, below is the approach.
我找到了解决方案。 将 IsDocumentEnabled 设置为“True”并将 IsReadOnly 设置为“True”。
完成此操作后,当我将鼠标悬停在超链接标记中显示的文本上时,鼠标将变成“手”。 在不按住控件的情况下单击将触发“Click”事件。
我正在使用 .NET 4 中的 WPF。我不知道早期版本的 .NET 是否无法按照我上面描述的方式运行。
I found a solution. Set IsDocumentEnabled to "True" and set IsReadOnly to "True".
Once I did this, the mouse would turn into a 'hand' when I hover over a text displayed within a HyperLink tag. Clicking without holding control will fire the 'Click' event.
I am using WPF from .NET 4. I do not know if earlier versions of .NET do not function as I describe above.
JHubbard80的答案是一个可能的解决方案,如果您不需要选择内容,这是最简单的方法。
但是我需要:P 这是我的方法:为
RichTextBox
内的Hyperlink
设置样式。 本质是使用EventSetter
使Hyperlink
处理MouseLeftButtonDown
事件。在代码隐藏中:
感谢 gcores 的启发。
JHubbard80's answer is a possible solution, it's the easiest way if you do not need the content to be selected.
However I need that :P here is my approach: set a style for the
Hyperlink
s inside theRichTextBox
. The essential is to use aEventSetter
to make theHyperlink
s handling theMouseLeftButtonDown
event.And in codebehind:
Thanks to gcores for the inspiaration.
几乎是偶然地,设法找到了解决这个问题的方法。
加载到 RichTextBox 中的内容只是作为纯字符串存储(或输入)。 我已经对 RichTextBox 进行了子类化,以允许绑定它的 Document 属性。
与问题相关的是,我有一个 IValueConverter Convert() 重载,看起来像这样(对解决方案来说非必要的代码已被删除):
这让我得到了我想要的行为(将纯字符串推入 RichTextBox 并获取格式),并且它还会导致链接的行为类似于普通链接,而不是嵌入到 Word 文档中的链接。
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):
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.
我从 @hillin 的答案中更改了 EventSetter。
MouseLeftButtonDown 在我的代码(.Net Framework 4.5.2)中不起作用。
I changed EventSetter from @hillin's answer.
MouseLeftButtonDown didn't work in my code (.Net framework 4.5.2).
我的答案基于 @BionicCode 的答案,我想用事件处理程序代码来扩展它,但我在让它工作时遇到了一些困难。
我稍微改变了他的代码:
RichTextBox
是只读的。 当RichTextBox
为只读时,无需将HyperLink
放入TextBlock
中。 但是,在用户可以进行更改的RichTextBlock
中使用TextBlock
是一个很好的建议。超链接
命名就足够了。代码隐藏
我需要在
HelpWindow
中显示一些带有链接的富文本:请注意,任何
HyperLink
都可以使用相同的事件处理程序。 另一种解决方案是不在 XAML 中定义 URL,而是在事件处理程序中对其进行硬编码,在这种情况下,每个HyperLink
都需要自己的事件处理程序。在各种 Stackoverflow 答案中,我看到了代码:
这导致了错误消息:
System.ComponentModel.Win32Exception: '尝试启动进程 'https://duckduckgo.com/' 时发生错误目录“...\bin\Debug\net6.0-windows”。 该系统找不到指定的文件。'
My answer is based on @BionicCode's answer, which I wanted to extend with the event handler code, which I had some difficulties to get it working.
I changed his code slightly:
RichTextBox
to be readonly. When theRichTextBox
is readonly, it is not necessary to put theHyperLink
into aTextBlock
. However, usingTextBlock
in aRichTextBlock
where the user can make changes is a great suggestion.Hyperlink
a name.Code behind
I needed to display some rich text with links in a
HelpWindow
:Note that the same event handler can be used by any
HyperLink
. Another solution would be not to define the URL in XAML but hard code it in the event handler, in which case eachHyperLink
needs its own event handler.In various Stackoverflow answers I have seen the code:
Which resulted in the error message:
System.ComponentModel.Win32Exception: 'An error occurred trying to start process 'https://duckduckgo.com/' with working directory '...\bin\Debug\net6.0-windows'. The system cannot find the file specified.'