将 WPF 超链接控件与动态 URL 结合使用
免责声明:我是 WPF 的新手,所以我对这个愚蠢的问题表示歉意。
我有一个包含超链接标记的 WPF 表单,如果 URL 硬编码到 XAML 中,该标记可以正常工作,如下所示:
<TextBlock Margin="171,148,129,70">
<Hyperlink NavigateUri="http://www.somesite.com" RequestNavigate="Hyperlink_RequestNavigate">
<TextBlock Text="Open site in browser" />
</Hyperlink>
</TextBlock>
后面带有以下代码:
protected void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}
我想要做的是使该超链接的目标 URL 动态化。我添加了一个新窗口(作为对话框调用)来获取新链接,然后在其“true”返回时将其分配给主窗口类的私有成员。如何调整 XAML 标记/代码隐藏以始终使用类成员(我将在构造函数中设置为默认值),而不是将其硬编码到标记中?
预先感谢,并对菜鸟问题表示歉意。我一定是使用了错误的搜索词。
Disclaimer: I am really new at WPF, so I apologize for the dumb question.
I have a WPF form that contains a hyperlink tag that works fine if the URL is hard-coded into the XAML, like so:
<TextBlock Margin="171,148,129,70">
<Hyperlink NavigateUri="http://www.somesite.com" RequestNavigate="Hyperlink_RequestNavigate">
<TextBlock Text="Open site in browser" />
</Hyperlink>
</TextBlock>
With the following code behind:
protected void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}
What I would like to do is make this hyperlink's destination URL dynamic. I have added a new window (invoked as a dialog) to obtain the new link which is then assigned to a private member of the main window's class on its "true" return. How can I adjust the XAML markup/code-behind to use the class member at all times (I'll set to a default in the constructor) instead of hard-coding it into the tag?
Thanks in advance, and sorry for the noob question. I must be using the wrong search terms.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用看起来像超链接的按钮并将
CommandParameter
绑定到您的 URL。然后,您可以设置Command
来运行如下所示的操作:You could use a Button styled to look like a Hyperlink and bind the
CommandParameter
to your URL. You'd then set theCommand
to run something like this:只需更改您的超链接 _requestNavigate 方法即可使用该私有变量:
Just change your hyperlink _requestNavigate method to use that private variable: