且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

wpf:如何在运行时添加超链接?

更新时间:2023-12-06 14:57:16

有点笨拙,但你需要这样做:

It's a little bit kludgy, but you need to do this:

Label linkLabel = new Label();
Run linkText = new Run("Google");
Hyperlink link = new Hyperlink(linkText);

link.NavigateUri = new Uri("http://www.google.com");

link.RequestNavigate += new RequestNavigateEventHandler(delegate(object sender, RequestNavigateEventArgs e) {
    Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
    e.Handled = true; 
});

linkLabel.Content = link;

myStackPanel.Children.Add(linkLabel);

这将创建一个带有文本Google"的新标签,Uri "http://www.google.com",点击后会在用户的默认浏览器中打开 Uri.

This will make a new label with the text "Google", the Uri "http://www.google.com", and when clicked it will open the Uri in the user's default browser.