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屬性來綁定要顯示的文字數據。
參考資料: