Little nugget of learning on Hyperlink controls in WPF

Hyperlinks in WPF

Using the Hyperlink control...

Hyperlink is a nifty control, possibly more useful in xbap (browser-based) applications than in Windows ones, but useful nevertheless for navigating around your app.

Here's a little example of how great it is to be using a Microsoft development platform on a Windows machine, and the little bits you get for free...

In the prototype I'm helping to create at the moment, a hyperlink is showing within a particular column in a datagrid (yes, yes, I know...) so, anyway, we needed to handle clicks from within the view, like this:

public partial class FooView : UserControl
{
    public FooView()
    {
        InitializeComponent();

        AddHandler(Hyperlink.ClickEvent, new RoutedEventHandler(HyperlinkClicked));
    }

    private void HyperlinkClicked(object sender, RoutedEventArgs e)
    {
    }
}

Now we need to actually handle the click... so in the HyperlinkClicked method, we write something like:

    private void HyperlinkClicked(object sender, RoutedEventArgs e)
    {
        var hyperlink = e.OriginalSource as Hyperlink);
        if (hyperlink == null) return;
        var navigateUri = hyperlink.NavigateUri;
        if (navigateUri == null) return;

        var applicationLink = UrlConverter.ParseUri(navigateUri);
        if (applicationLink != null)
        {
            (this.DataContext as FooViewModel).ClickLink(applicationLink);
            e.Handled = true;
        }
    }

The call to ParseUri just uses a bit of regex to parse out the Uri - we expect it to look like: "foo://{0}/{1}", type, id" so we can 'route' the clicks to open the correct window with the correct data loaded up.

Now here comes the magic. Our app is an Orgasmic Mirror System, or Orange Monkey System or something, and in the manner of all truly genius-creative devs, we lovingly refer to it as OMS, so what better naming convention to apply to the Uri than "oms://{0}/{1}", type, id", right?

Turns out, Microsoft Outlook called shotgun on 'oms' - the following conversation is, as near as I can remember, a true and accurate reflection of earlier in the day between Mark and I:

Mark: What on earth...?

Me: Uh?

Mark: Why is Outlook asking me if I want to add an account in Microsoft Outlook for this service?

Me: Huh?

Mark: Look. I've just put oms://customers/1123 in a hyperlink and Outlook is prompting me for something...

Outlook says wha'?

Me: Wha?

Mark: Hm. Wonder what 'oms' is? *Googles 'oms' with Bing and finds hit for 'Outlook Mobile Services'. So, what would happen if I just put 'http://' in place

Me: Sorry, what?

Mark: Look at that. It just opens up a browser. I didn't have to tell it anything about how I wanted that Uri handled, I just gave it an appropriate protocol..

Me: Urrrr...

Mark: See, it even works with 'mailto:' - clicking it just opens up an email and puts customers/1123 in the 'To' field. Cool!

Me: ... you're welcome?

Sitting next to smart people is great - I learn loads. Working with amazing, integrated technology stacks that do so much heavy lifting for you, if only you know which buttons to hit... even more so!

wpf
Posted by: Ian Randall
Last revised: 02 Nov, 2011 07:53 a.m.

Comments

No comments yet. Be the first!

Your Comments

Used for your gravatar. Not required. Will not be public.
Posting code? Indent it by four spaces to make it look nice. Learn more about Markdown.

Preview