2009-02-10 33 views
45

我想要做的就是在XAML中創建一個超級鏈接。我已經嘗試了一切。我放棄。如何在XAML中製作一個簡單的超鏈接?

這是什麼語法?

<StackPanel Width="70" HorizontalAlignment="Center"> 

    <Hyperlink Click="buttonClose_Click" Cursor="Hand" 
     Foreground="#555" Width="31" Margin="0 0 0 15" 
     HorizontalAlignment="Right">Close</Hyperlink> 

    <Button Width="60" Margin="0 0 0 3">Test 1</Button> 
    <Button Width="60" Margin="0 0 0 3">Test 2</Button> 
    <Button Width="60" Margin="0 0 0 3">Test 3</Button> 
    <Button Width="60" Margin="0 0 0 3">Test 4</Button> 
</StackPanel> 

Visual Studio團隊:在Visual Studio 2010我想大眼夾彈出,並說:「看來你正試圖使一個超鏈接」,告訴我該怎麼做。你不能用MEF做到這一點嗎?這將是復古很酷的,這些小小的「如何做我已經知道如何做的HTML」問題在XAML的學習過程中耗費了太多時間。

+5

+1 - 「我怎麼做我已經知道如何在HTML做」的問題時與XAML學習過程中燒了這麼多的時間。 「-Man你沒有錯, – 2010-08-18 07:18:27

+2

尊重,把Clippy變成VS是一個非常糟糕的主意!!你能想象那個令人討厭的回形針汲取你正在編程的東西,並提出無效的建議嗎?我認爲你可以避免這些問題通過學習WPF之前跳進去 – MikeKulls 2012-04-23 01:29:32

+1

所以那裏有你;在寫XAML的時候我總是感嘆「如果只有這個是css ......」 – nocarrier 2014-01-04 03:10:35

回答

35

您可以使用帶自定義控件模板的按鈕,下面的代碼是一個有限的超鏈接樣式按鈕(例如,它只支持文本超鏈接),但它可能會指向正確的方向。

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
<Page.Resources> 
<Style x:Key="Link" TargetType="Button"> 
    <Setter Property="VerticalAlignment" Value="Center"/> 
    <Setter Property="HorizontalAlignment" Value="Center"/> 
    <Setter Property="Cursor" Value="Hand"/> 
    <Setter Property="Foreground" Value="Blue"/> 
    <Setter Property="Background" Value="Transparent"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Button"> 
       <TextBlock TextDecorations="Underline" 
        Text="{TemplateBinding Content}" 
        Background="{TemplateBinding Background}"/> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsPressed" Value="True"> 
         <Setter Property="Foreground" Value="Red"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
</Page.Resources> 
<Button Content="Click Me!" Style="{StaticResource Link}"/> 
</Page> 
1

通常,超鏈接的含義是給出一個錨點以將用戶發送到另一個頁面或通常與另一個資源進行通信,因此它以這種方式實現,您必須指定該資源的位置,如下所示:

<HyperLink NavigateUri="http://www.site.com"> 
    Web Site 
</HyperLink> 

不過,我發現this blog後與被用作超鏈接並支持單擊事件自定義的TextBlock。

+1

這應該是超鏈接,而不是超鏈接 – dmo 2010-05-12 18:42:23

162

你不能添加超鏈接到StackPanel - 你會得到一個運行時錯誤。 (其實,我有點驚訝它不是編譯時錯誤。)這是因爲超鏈接不住在WPF的「控件」一側,<Button><StackPanel>以及其他東西放在屏幕的矩形塊上並下降從UIElement。相反,它生活在事物的「文本」一面,其中<Bold><Run><Paragraph>以及其他一般文字的東西,它們按行和段落自動換行並從TextElement下降。

一旦你意識到有兩個不同的佈局行爲的單獨的類層次結構,它是有道理的,超鏈接將在事物的「文本」一側(使中間有一個超鏈接段落,甚至對於超鏈接來換行換行)。

但是,不,當你剛開始的時候並不容易發現。

要混合兩個世界,並使用超鏈接作爲控件,您只需將其放入TextBlock中即可。 TextBlock的是包含文字上下的東西(即可以包含超鏈接)控制上下的東西(即可以在StackPanel中去):

<TextBlock><Hyperlink Click="buttonClose_Click">Close</Hyperlink></TextBlock> 
4

您可能會發現,如果你綁定到任何其他而不是簡單的文本值,您需要使用ContentPresenter否則不會顯示任何內容,如果您綁定到XML數據源,則可能會出現這種情況。

IsMouseOver的屬性觸發器給文本一個下劃線。

下面介紹了一個綁定到XML的示例。

<Style x:Key="JobNumberStyleButton" TargetType="{x:Type Button}"> 
    <Setter Property="VerticalAlignment" Value="Top"/> 
    <Setter Property="HorizontalAlignment" Value="Left"/> 
    <Setter Property="Cursor" Value="Hand"/> 
    <Setter Property="Background" Value="Transparent"/> 
    <Setter Property="Template"> 
    <Setter.Value> 
     <ControlTemplate TargetType="Button"> 
     <TextBlock> 
      <ContentPresenter 
      Margin="0,0,0,0" 
      ContentTemplate="{TemplateBinding ContentTemplate}" 
      Content="{TemplateBinding Content}" 
      ContentStringFormat="{TemplateBinding ContentStringFormat}" 
      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
      VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
      RecognizesAccessKey="False" 
      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
     </TextBlock> 
     </ControlTemplate> 
    </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
    <Trigger Property="IsMouseOver" Value="True"> 
     <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Button"> 
      <TextBlock Padding="0,0,0,0" Margin="0,0,0,0"> 
       <Underline> 
       <ContentPresenter 
        Margin="0,0,0,0" 
        ContentTemplate="{TemplateBinding ContentTemplate}" 
        Content="{TemplateBinding Content}" 
        ContentStringFormat="{TemplateBinding ContentStringFormat}" 
        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
        VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
        RecognizesAccessKey="False" 
        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
       </Underline> 
      </TextBlock> 
      </ControlTemplate> 
     </Setter.Value> 
     </Setter> 
    </Trigger> 
    </Style.Triggers> 
</Style> 
19

試試這個:

<TextBlock> 
    <Hyperlink RequestNavigate="Hyperlink_RequestNavigate" 
       NavigateUri="http://www.msn.com">MSN</Hyperlink> 
</TextBlock> 

private void Hyperlink_RequestNavigate(object sender, 
             System.Windows.Navigation.RequestNavigateEventArgs e) 
{ 
    System.Diagnostics.Process.Start("http://www.msn.com"); 
} 
2

您可以簡單地使用HyperlinkButton。 當它被點擊,URL將被顯示在Web瀏覽器:

<HyperlinkButton 
    NavigateUri="https://dev.windowsphone.com" 
    TargetName="_blank" 
    Content="Windows Phone Dev Center" /> 
4
<TextBlock> 
    <Hyperlink NavigateUri="{Binding YourUri}" RequestNavigate="YourRequestNavigate"> 
    <TextBlock Text="{Binding YourText}" /> 
    </Hyperlink> 
</TextBlock> 

這將linkify嵌套文本塊的任何綁定文本,我還沒有發現又一個更好的辦法,我想如果可能的話,第一個文本塊不在那裏。 這也適用於DataTemplates。

在UWP與mvvmcross
0

我使用這個

<HyperlinkButton Content="{Binding TextSource, ConverterParameter=MyUrl, Converter={StaticResource Language}, 
      FallbackValue=_MyUrl}" NavigateUri="http://www.google.com" />