在使用WPF例如超链接 [英] Example using Hyperlink in WPF

查看:117
本文介绍了在使用WPF例如超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过一些建议,您可以通过超链接控制。

I've seen several suggestions, that you can add hyperlink to WPF application through Hyperlink control.

下面是如何我想在我的code使用它:

Here's how I'm trying to use it in my code:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        mc:Ignorable="d" 
        x:Class="BookmarkWizV2.InfoPanels.Windows.UrlProperties"
        Title="UrlProperties" Height="754" Width="576">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition Height="40"/>
        </Grid.RowDefinitions>
        <Grid>
            <ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto" Grid.RowSpan="2">
                <StackPanel >
                    <DockPanel LastChildFill="True" Margin="0,5">
                        <TextBlock Text="Url:" Margin="5" 
                            DockPanel.Dock="Left" VerticalAlignment="Center"/>
                        <TextBox Width="Auto">
                            <Hyperlink NavigateUri="http://www.google.co.in">
                                    Click here
                            </Hyperlink>   
                        </TextBox>                      
                    </DockPanel >
                </StackPanel>
            </ScrollViewer>        
        </Grid>
        <StackPanel HorizontalAlignment="Right" Orientation="Horizontal" Margin="0,7,2,7" Grid.Row="1" >
            <Button Margin="0,0,10,0">
                <TextBlock Text="Accept" Margin="15,3" />
            </Button>
            <Button Margin="0,0,10,0">
                <TextBlock Text="Cancel" Margin="15,3" />
            </Button>
        </StackPanel>
    </Grid>
</Window>

我收到以下错误:

I'm getting following error:

属性文本不支持类型超链接的值。

Property 'Text' does not support values of type 'Hyperlink'.

我在做什么错了?

推荐答案

如果你希望你的应用程序打开一个 Web浏览器的链接你需要添加一个的HyperLink 与<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.documents.hyperlink.requestnavigate.aspx\">RequestNavigate事件设置为编程打开网络浏览器与该地址作为参数的函数。

If you want your application to open the link in a web browser you need to add a HyperLink with the RequestNavigate event set to a function that programmatically opens a web-browser with the address as a parameter.

<TextBlock>           
    <Hyperlink NavigateUri="http://www.google.com" RequestNavigate="Hyperlink_RequestNavigate">
        Click here
    </Hyperlink>
</TextBlock>

在code-背后,你需要添加类似这样的处理RequestNavigate事件东西。

In the code-behind you would need to add something similar to this to handle the RequestNavigate event.

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

此外,您还需要以下的进口。

In addition you will also need the following imports.

using System.Diagnostics;
using System.Windows.Navigation;

它看起来像这样在你的应用程序。

It would look like this in your application.

这篇关于在使用WPF例如超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆