I need to add several hyperlinks to the program's form dynamically at runtime (and handle user clicks on them).
我需要在运行时动态地向程序的表单添加几个超链接(并处理用户对它们的点击)。
How to do this?
这个怎么做?
I tried something like: var hlink = new Hyperlink(); myStackPanel.Children.Add(hlink);
我尝试过类似的东西:var hlink = new Hyperlink(); myStackPanel.Children.Add(HLINK);
but hyperlink is not a UIElement...
但超链接不是UIElement ...
Thanks!
谢谢!
2 个解决方案
#1
3
It's a little bit kludgy, but you need to do this:
它有点kludgy,但你需要这样做:
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);
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.
这将创建一个带有文本“Google”的新标签,即Uri“http://www.google.com”,点击后它将在用户的默认浏览器中打开Uri。
#2
2
Hyperlink is not a UI Element but you can add it as content into a Label.
超链接不是UI元素,但您可以将其作为内容添加到Label中。
In Xaml it will look like this:
在Xaml中它看起来像这样:
<Label>
<Hyperlink Click="btnRemoveAll_Click">Remove all</Hyperlink>
</Label>
You can use it for many things just as you would use the click-event of a button.
您可以将它用于许多事情,就像使用按钮的单击事件一样。
#1
3
It's a little bit kludgy, but you need to do this:
它有点kludgy,但你需要这样做:
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);
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.
这将创建一个带有文本“Google”的新标签,即Uri“http://www.google.com”,点击后它将在用户的默认浏览器中打开Uri。
#2
2
Hyperlink is not a UI Element but you can add it as content into a Label.
超链接不是UI元素,但您可以将其作为内容添加到Label中。
In Xaml it will look like this:
在Xaml中它看起来像这样:
<Label>
<Hyperlink Click="btnRemoveAll_Click">Remove all</Hyperlink>
</Label>
You can use it for many things just as you would use the click-event of a button.
您可以将它用于许多事情,就像使用按钮的单击事件一样。