wpf 如何在 XAML 中创建一个简单的超链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/531621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a simple hyperlink in XAML?
提问by Edward Tanguay
All I want to do is make a little hyperlink in XAML. I've tried everything. I give up.
我想要做的就是在 XAML 中创建一个小超链接。我什么都试过了。我放弃。
What is the syntax for this?
这是什么语法?
<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 Team: In Visual Studio 2010 I want Clippy to pop up and say "It seems you are trying to make a hyperlink" and tell me how to do it. Can't you do that with MEF? It would be retro cool, and these little "how do I do what I already know how to do in HTML" issues burn up so much time during the learning process with XAML.
Visual Studio 团队:在 Visual Studio 2010 中,我希望 Clippy 弹出并说“您似乎正在尝试制作超链接”并告诉我如何操作。你不能用 MEF 做到这一点吗?这会很酷,而且这些小问题“我如何做我已经知道如何在 HTML 中做的事情”在使用 XAML 的学习过程中消耗了很多时间。
采纳答案by Nir
You can use a Button with a custom control template, the code below is a limited hyperlink style button (for example it only support textual hyperlinks) but maybe it'll point you in the right direction.
您可以将 Button 与自定义控件模板一起使用,下面的代码是一个有限的超链接样式按钮(例如它只支持文本超链接),但也许它会为您指明正确的方向。
<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>
回答by Joe White
You can't add a Hyperlink to a StackPanel -- you'll get a runtime error. (Actually, I'm kinda surprised it's not a compile-time error.) That's because Hyperlink doesn't live in the "controls" side of WPF with <Button>
and <StackPanel>
and other things that are laid out on rectangular chunks of screen and descend from UIElement
. Instead, it lives in the "text" side of things, with <Bold>
and <Run>
and <Paragraph>
and other generally texty things that word-wrap and flow in lines and paragraphs and descend from TextElement
.
您无法向 StackPanel 添加超链接——您将收到运行时错误。(实际上,我有点惊讶这不是编译时错误。)那是因为超链接不存在于 WPF 的“控件”端,<Button>
并且<StackPanel>
还有其他东西,它们布置在屏幕的矩形块上并从UIElement
. 相反,它存在于事物的“文本”方面,具有<Bold>
和<Run>
和<Paragraph>
以及其他一般文本性的事物,它们在行和段落中自动换行和流动并从TextElement
.
Once you realize that there are two separate class hierarchies with different layout behaviors, it makes sense that Hyperlink would be on the "text" side of things (makes it easy to e.g. have a paragraph with a hyperlink in the middle, and even for that hyperlink to wrap across a line break).
一旦您意识到有两个具有不同布局行为的独立类层次结构,超链接将位于事物的“文本”一侧是有意义的(例如,很容易在中间有一个带有超链接的段落,甚至对于那个换行的超链接)。
But no, it's not so discoverable when you're starting out.
但是不,当您开始时,它并不是那么容易被发现。
To mix the two worlds, and use a hyperlink as a control, all you need to do is put it in a TextBlock. TextBlock is a control-ish thing (i.e., can go in a StackPanel) that contains text-ish things (i.e., can contain a Hyperlink):
要混合这两个世界,并使用超链接作为控件,您需要做的就是将它放在一个 TextBlock 中。TextBlock 是一个控件类的东西(即,可以进入 StackPanel),它包含文本类的东西(即,可以包含一个超链接):
<TextBlock><Hyperlink Click="buttonClose_Click">Close</Hyperlink></TextBlock>
回答by Nalan Madheswaran
Try this:
尝试这个:
<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(e.Uri.AbsoluteUri);
}
回答by VampireMonkey
<TextBlock>
<Hyperlink NavigateUri="{Binding YourUri}" RequestNavigate="YourRequestNavigate">
<TextBlock Text="{Binding YourText}" />
</Hyperlink>
</TextBlock>
This will linkify any binded text in the nested textblock, i have not found a better way yet, i would like the first textblock to not be there if possible. This will work for DataTemplates aswell.
这将链接嵌套文本块中的任何绑定文本,我还没有找到更好的方法,如果可能,我希望第一个文本块不在那里。这也适用于 DataTemplates。
回答by Brett Ryan
You may find that if you're binding to anything other than simple text values you will need to use ContentPresenter
otherwise nothing will appear, this could be true if you're binding to an XML data source.
您可能会发现,如果绑定到除简单文本值之外的任何内容,则需要使用ContentPresenter
否则不会出现任何内容,如果绑定到 XML 数据源,则可能是这样。
A Property Trigger for IsMouseOver gives the text an underline.
IsMouseOver 的属性触发器为文本提供下划线。
An example where I"m binding to XML is presented below.
下面给出了我绑定到 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>
回答by Spaso Lazarevic
You can simply use HyperlinkButton. When it is clicked on, the URL will be displayed in your web browser:
您可以简单地使用HyperlinkButton。单击它时,URL 将显示在您的 Web 浏览器中:
<HyperlinkButton
NavigateUri="https://dev.windowsphone.com"
TargetName="_blank"
Content="Windows Phone Dev Center" />
回答by Stefano Driussi
Usually, the meaning of an Hyperlink is to give an anchor to send the user to another Page or generally speaking to another resource, so it's implemented in such a way and you have to specify the location for that resource like this:
通常,超链接的含义是提供一个锚点来将用户发送到另一个页面或一般来说另一个资源,因此它以这种方式实现,您必须像这样指定该资源的位置:
<HyperLink NavigateUri="http://www.site.com">
Web Site
</HyperLink>
However, i've found this blogpost with a custom TextBlock that is used as an HyperLink and supports click events.
但是,我发现这篇博客文章带有一个自定义 TextBlock,它用作超链接并支持点击事件。
回答by El0din
in UWP with mvvmcross i'm using this
在带有 mvvmcross 的 UWP 中,我正在使用它
<HyperlinkButton Content="{Binding TextSource, ConverterParameter=MyUrl, Converter={StaticResource Language},
FallbackValue=_MyUrl}" NavigateUri="http://www.google.com" />