将 WPF 超链接控件与动态 URL 结合使用

发布于 2024-11-05 13:17:24 字数 806 浏览 0 评论 0原文

免责声明:我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

厌倦 2024-11-12 13:17:24

您可以使用看起来像超链接的按钮并将 CommandParameter 绑定到您的 URL。然后,您可以设置 Command 来运行如下所示的操作:

public void OpenWebsite(string url)
{ 
    Process.Start(url);
}

You could use a Button styled to look like a Hyperlink and bind the CommandParameter to your URL. You'd then set the Command to run something like this:

public void OpenWebsite(string url)
{ 
    Process.Start(url);
}
夜巴黎 2024-11-12 13:17:24

只需更改您的超链接 _requestNavigate 方法即可使用该私有变量:

protected void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
    Process.Start(new ProcessStartInfo(privateVariableName));
    e.Handled = true;
}

Just change your hyperlink _requestNavigate method to use that private variable:

protected void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
    Process.Start(new ProcessStartInfo(privateVariableName));
    e.Handled = true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文