Infovark Underground

  • news
    • infoblog
    • underground
  • product
  • download
  • buy
  • support
  • about
    • ← An analysis of our database schema
    • Getting XAML Hyperlink text to wrap →

    How to format the XAML Hyperlink NavigateUri

    30 Dec 2010 by Dean in .NET, WPF / No Comments

    Have you ever wanted to construct a dynamic query string for your Hyperlinks in WPF or Silverlight? Perhaps it’s the web developer in me, but I find myself wanting to do this all the time.

    Sadly, the Hyperlink element’s NavigateUri property makes this extremely difficult. Since the NavigateUri property’s target type is a Uri, not a string, you can’t use the convenient StringFormat syntax to create dynamic URLs. I started a StackOverflow question to discuss how to use StringFormat with the NavigateUri property. The best approach for now appears to be to write an IValueConverter that will create the Uri from a string.

    This works if you only need to apply a single value. But what if you need to supply multiple values to generate a querystring with several parameters?

    A post from Paul Stovell pointed me in the right direction: prior to the introduction of special StringFormat syntax in .NET 3.5 SP1, you needed to use multibinding to manipulate strings. We can use this older method to generate the Uri we need for the Hyperlink.

    First, create a converter that implements IMultiValueConverter.

    1.     [ValueConversion(typeof(String), typeof(Uri))]
    2.     public class StringToUriConverter : IMultiValueConverter
    3.     {
    4.         public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    5.         {
    6.             Uri result;
    7.             string input = string.Format(parameter.ToString(), values);
    8.  
    9.             Uri.TryCreate(input, UriKind.RelativeOrAbsolute, out result);
    10.             return result;
    11.         }
    12.  
    13.         public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    14.         {
    15.             throw new NotImplementedException("This converter cannot be used in two-way binding.");
    16.         }
    17.     }

    Remember to add the converter application resources in your App.xaml file, so you can reference it throughout your application.

    1. <Application.Resources>
    2.  <ResourceDictionary>
    3.   <Manager:StringToUriConverter x:Key="stringToUri"/>
    4.  </ResourceDictionary>
    5. </Application.Resources>

    Then you can use it wherever you need to dynamically create a URI within your WPF or Silverlight application.

    1. <TextBlock>
    2.  <Hyperlink>
    3.   <Hyperlink.NavigateUri>
    4.    <MultiBinding ConverterParameter="/Projects/ItemIndexPage.xaml?ProjectId={0}&amp;TemplateType={1}"
    5.          Converter="{StaticResource stringToUri}">
    6.     <Binding ElementName="ProjectId" Path="Text"/>
    7.     <Binding Path="Template"/>
    8.    </MultiBinding>
    9.   </Hyperlink.NavigateUri>
    10.   <Run Text="{Binding LinkText}"/>
    11.  </Hyperlink>
    12. </TextBlock>

    Whew. That’s a lot of extra work just to generate a URL for a hyperlink!

    Notice that I had to escape the ampersand in the format string supplied in the ConverterParameter. Since XAML is based on XML, not HTML, you must escape the ampersand within attribute values. This catches me every time.

    Generating dynamic URLs is such a basic operation for building navigation-style WPF or Silverlight applications, I’m surprised the folks at Microsoft didn’t create a helper syntax to handle this cleanly.

    Related posts

    1. Getting XAML Hyperlink text to wrap
    2. Highlighting query terms in a WPF TextBlock
    3. Using WCF to return HTML
    4. Switch By Type in C#
    5. WCF WebInvoke Body Format Error
    • Tweet
    • Tags:
    • hyperlink
    • WPF
    • xaml

    Leave a Comment

    Posting your comment...

    Subscribe to these comments via email

    • Categories

      • .NET (41)
      • AJAX (3)
      • Books (7)
      • HTML (9)
      • Infovark (8)
      • Programming (48)
      • REST (11)
      • SQL (3)
      • Testing (3)
      • Tools (13)
      • UI (3)
      • WCF (11)
      • Web Services (8)
      • WPF (4)
      • XML (4)
    • Archives

    • Get future articles


       

    • Blogroll

      • Ajaxian
      • Anne Van Kesteren
      • Brain.Save()
      • Coding Horror
      • Eric Sink
      • Joel Spolsky
      • John Resig
      • Mark Pilgrim
      • Raymond Chen
      • Scott Hansleman
      • Secret Geek
      • Steve Yegge
      • The Daily WTF
      • The Database Programmer
    • Meta

      • Log in
      • Entries RSS
      • Comments RSS
      • WordPress.org
  • Site map

    • News
    • Product
    • Download
    • Buy
    • Support
    • About
  • Recent Posts

    • Review: Brownfield Application Development in .NET
    • Using Modal Dialogs with a Splash Screen in WPF
    • Highlighting query terms in a WPF TextBlock
    • Getting XAML Hyperlink text to wrap
    • How to format the XAML Hyperlink NavigateUri
  • Twitter

    Copyright 2011 Infovark, Inc. All rights reserved.