【WPF】绑定Hyperlink超链接

Hyperlink超链接的简单使用

前台XAML:

    <TextBlock>
        说明文字:
        <Hyperlink NavigateUri="http://www.qq.com" Click="Hyperlink_Click">www.baidu.com</Hyperlink>
    </TextBlock>

后台代码实现点击超链接的逻辑:

        private void Hyperlink_Click(object sender, RoutedEventArgs e)
        {
            Hyperlink link = sender as Hyperlink;
         // 激活的是当前默认的浏览器
            Process.Start(new ProcessStartInfo(link.NavigateUri.AbsoluteUri));
        }

虽然显示的网址是X度,但其实去到的是疼迅,控件运行起来是这个样子:

参考资料:


再进一步:动态创建Hyperlink超链接控件

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);

因为Hyperlink控件本身不是UIElement,所以无法直接被加到控件上。需要包裹在一个Label标签中,然后再把Label加到界面控件上显示。

参考资料:


更进一步:Hyperlink在XAML中的绑定

<TextBlock>
    <Hyperlink Command="local:MyCommands.ViewDetails" CommandParameter="{Binding}">
        <TextBlock Text="{Binding Path=Name}"/>
    </Hyperlink>
</TextBlock>

要想绑定显示的文字内容,需要在<Hyperlink>标签内加一个<TextBlock>标签,该<TextBlock>标签的Text属性来绑定要显示的文字数据。

参考资料:


原文地址:https://www.cnblogs.com/guxin/p/csharp-wpf-hyperlink-data-binding.html