带链接的C#WPF文本 [英] C# WPF Text with links

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

问题描述

我刚刚发现自己是一个新的挑战:使文字处理程序比纯文本更像是在处理网络.为此,我迫不及待想设计一个不错的框架,但是我确实需要知道GUI方面的可能性(它可能会带来许多GUI挑战).

I just found myself a new challenge: Make a Word Processor that is in handling more like the web than plain text. Designing a nice framework for this is what i cant wait to start with, but i do need to know what the possibilities are at the GUI side (it will probably have loads of GUI challenges).

因此,最基本的东西是我需要某种控制,以便可以使部分文本可点击/鼠标悬停.

我是WPF的新手,不确定如何执行此操作.有谁知道如何做到这一点?有例子吗?已经有控制了吗?

I'm kinda new to WPF and not sure how to do this. Has anybody an idea how to make this? Are there examples? Are there already controls for this?

预先感谢

我找到了一种使用Richtextbox做到这一点的方法:

I found out some way to do it with a richtextbox:

// Create a FlowDocument to contain content for the RichTextBox.
FlowDocument myFlowDoc = new FlowDocument();

// Add paragraphs to the FlowDocument.

Hyperlink myLink = new Hyperlink();
myLink.Inlines.Add("hyperlink");
myLink.NavigateUri = new Uri("http://www.stackoverflow.com");

// Create a paragraph and add the Run and hyperlink to it.
Paragraph myParagraph = new Paragraph();
myParagraph.Inlines.Add("check this link out: ");
myParagraph.Inlines.Add(myLink);
myFlowDoc.Blocks.Add(myParagraph);

// Add initial content to the RichTextBox.
richTextBox1.Document = myFlowDoc;

我现在在我的文本框中得到一个漂亮的超链接……除非单击它,否则什么也没有发生.我在这里想念什么?

I now get a nice hyperlink in my textbox... except when i click it, nothing happens. what am i missing here?

推荐答案

您可以使用

You can use the Hyperlink class. It's a FrameworkContentElement, so you can use it in a TextBlock or FlowDocument or anywhere else you can embed content.

<TextBlock>
    <Run>Text</Run>
    <Hyperlink NavigateUri="http://stackoverflow.com">with</Hyperlink>
    <Run>some</Run>
    <Hyperlink NavigateUri="http://google.com">hyperlinks</Hyperlink>
</TextBlock>

您可能希望使用 RichTextBox 作为您的编辑器的一部分.这将托管一个FlowDocument,其中可以包含诸如超链接之类的内容.

You may want to look at using a RichTextBox as part of your editor. This will host a FlowDocument, which can contain content such as Hyperlinks.

更新:有两种方法可以处理对超链接的单击.一种是处理 RequestNavigate 事件.这是路由事件,因此您可以将处理程序附加到超链接本身,或者您可以将其附加到树中更高的元素,例如Window或RichTextBox:

Update: There are two ways to handle clicks on the Hyperlink. One is to handle the RequestNavigate event. It is a Routed Event, so you can either attach a handler to the Hyperlink itself or you can attach one to an element higher in the tree such as the Window or the RichTextBox:

// On a specific Hyperlink
myLink.RequestNavigate +=
    new RequestNavigateEventHandler(RequestNavigateHandler);
// To handle all Hyperlinks in the RichTextBox
richTextBox1.AddHandler(Hyperlink.RequestNavigateEvent,
    new RequestNavigateEventHandler(RequestNavigateHandler));

另一种方法是通过设置来使用命令 Command 属性href ="http://msdn.microsoft.com/zh-cn/library/system.windows.input.icommand.aspx" rel ="noreferrer"> ICommand 实现.单击超链接时,将在ICommand上调用Executed方法.

The other way is to use commanding by setting the Command property on the Hyperlink to an ICommand implementation. The Executed method on the ICommand will be called when the Hyperlink is clicked.

如果要在处理程序中启动浏览器,可以将URI传递给 Process.Start :

If you want to launch a browser in the handler, you can pass the URI to Process.Start:

private void RequestNavigateHandler(object sender, RequestNavigateEventArgs e)
{
    Process.Start(e.Uri.ToString());
}

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

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