Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a simple hyperlink in XAML?

All I want to do is make a little hyperlink in XAML. I've tried everything. I give up.

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.

like image 800
Edward Tanguay Avatar asked Feb 10 '09 09:02

Edward Tanguay


People also ask

How to make a Hyperlink in XAML?

In XAML, the creation of content elements is implicit, so you can add the link text directly to the Hyperlink, and the Hyperlink directly to the TextBlock element. The Span element with the xml:space="preserve" attribute is used to preserve white space around the hyperlink.

How do I add a link button in WPF?

Hello, Link button in ASP.NET at runtime works like a hyperlink, but you can write certain code with the use of a link button by generating its click event, in short the link button control looks like a hyperlink control but works like a button... Here is some code that might help you out.

What is default appearance of hyperlink text to different browsers?

By default, hyperlinks are underlined.

What is in XAML?

XAML stands for Extensible Application Markup Language. It's a simple and declarative language based on XML. In XAML, it very easy to create, initialize, and set properties of objects with hierarchical relations.


1 Answers

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.

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><Hyperlink Click="buttonClose_Click">Close</Hyperlink></TextBlock> 
like image 176
Joe White Avatar answered Sep 23 '22 05:09

Joe White