2012-04-20 51 views
112

我見過幾個建議,您可以通過Hyperlink控件將超鏈接添加到WPF應用程序。使用WPF中的超鏈接示例

下面是我想在我的代碼使用它:

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

我收到以下錯誤:

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

我在做什麼錯?

回答

246

如果你希望你的應用程序中打開你需要添加一個HyperLinkRequestNavigate事件設置爲編程打開一個網絡瀏覽器的地址作爲參數的函數web browser的鏈接。

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

在代碼隱藏中,您需要添加類似於此的內容來處理RequestNavigate事件。

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

此外,您還需要以下導入。

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

它會在你的應用程序中看起來像這樣。

oO

+4

注意:'RequestNavigateEventArgs'是在'System.Windows.Navigation'命名空間。 – Ben 2013-10-12 17:55:53

+2

謝謝,但有沒有什麼辦法通過綁定指定鏈接文本(在這裏是「點擊這裏」)? – Agent007 2013-12-26 06:52:49

+5

只需再次把超鏈接內的文本塊和綁定Textproperty – KroaX 2014-03-12 15:09:45

24

Hyperlink控制,它是一個flow content元素,你只能在支持流的內容,就像一個TextBlock控件使用它。 TextBoxes只能有純文本。

51

除了富士的反應,我們可以使處理器可重用把它變成一個附加屬性:

public static class HyperlinkExtensions 
{ 
    public static bool GetIsExternal(DependencyObject obj) 
    { 
     return (bool)obj.GetValue(IsExternalProperty); 
    } 

    public static void SetIsExternal(DependencyObject obj, bool value) 
    { 
     obj.SetValue(IsExternalProperty, value); 
    } 
    public static readonly DependencyProperty IsExternalProperty = 
     DependencyProperty.RegisterAttached("IsExternal", typeof(bool), typeof(HyperlinkExtensions), new UIPropertyMetadata(false, OnIsExternalChanged)); 

    private static void OnIsExternalChanged(object sender, DependencyPropertyChangedEventArgs args) 
    { 
     var hyperlink = sender as Hyperlink; 

     if ((bool)args.NewValue) 
      hyperlink.RequestNavigate += Hyperlink_RequestNavigate; 
     else 
      hyperlink.RequestNavigate -= Hyperlink_RequestNavigate; 
    } 

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

而且使用這樣的:

<TextBlock> 
<Hyperlink NavigateUri="http://stackoverflow.com" custom::HyperlinkExtensions.IsExternal="true"> 
     Click here 
    </Hyperlink> 
</TextBlock> 
3

我喜歡亞瑟的可重用處理程序的想法,但我認爲有一個更簡單的方法來做到這一點:

private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e) 
{ 
    if (sender.GetType() != typeof (Hyperlink)) 
     return; 
    string link = ((Hyperlink) sender).NavigateUri.ToString(); 
    Process.Start(link); 
} 

很明顯,啓動任何類型的進程都可能存在安全風險,因此請謹慎對待。

7

恕我直言,最簡單的方法是使用新的控制從Hyperlink繼承:

/// <summary> 
/// Opens <see cref="Hyperlink.NavigateUri"/> in a default system browser 
/// </summary> 
public class ExternalBrowserHyperlink : Hyperlink 
{ 
    public ExternalBrowserHyperlink() 
    { 
     RequestNavigate += OnRequestNavigate; 
    } 

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

如果您想稍後本地化的字符串,那麼這些問題的答案是不夠的,我建議是這樣的:

<TextBlock> 
    <Hyperlink NavigateUri="http://labsii.com/"> 
     <Hyperlink.Inlines> 
      <Run Text="Click here"/> 
     </Hyperlink.Inlines> 
    </Hyperlink> 
</TextBlock> 
1

希望這可以幫助別人。

using System.Diagnostics; 
using System.Windows.Documents; 

namespace Helpers.Controls 
{ 
    public class HyperlinkEx : Hyperlink 
    { 
     protected override void OnClick() 
     { 
      base.OnClick(); 

      Process p = new Process() 
      { 
       StartInfo = new ProcessStartInfo() 
       { 
        FileName = this.NavigateUri.AbsoluteUri 
       } 
      }; 
      p.Start(); 
     } 
    } 
} 
4

請注意,Hyperlink不一定非要用於導航。你可以將它連接到一個命令。

例如:

<TextBlock> 
    <Hyperlink Command="{Binding ClearCommand}">Clear</Hyperlink> 
</TextBlock>