1
votes

No problem when binding to a property of string type ( "http://something.com" ). However , I seem to have seen in old examples direct binding to Uri property.

        <dg:DataGridHyperlinkColumn IsReadOnly="True"
                    Header="Uri" Binding="{Binding Path=NavigURI}" />

NavigURI is Uri . More recent docs seem to require a converter

<DataGridHyperlinkColumn Header="Email" Binding="{Binding Email}"  ContentBinding="{Binding Email, Converter={StaticResource EmailConverter}}" />

I tried with a converter also, but in both cases with or without converter column is empty. Debugging showed that value passed to "Convert" method is always null. My question : if for any reason I want binding to Uri property , is it feasible for the latest DataGrid from Codeplex ?

1
In your second example why are you binding it to ContentBinding ?? why not include converted in Binding property?Akash Kava
That's what I saw at msdn msdn.microsoft.com/en-us/library/…MicMit

1 Answers

6
votes

The ContentBinding property can be used to display text other than the actual URL. For example, if I am binding to data with friendly, human-readable text in ProductName and the actual URL in ProductUri, I might do this:

<DataGridHyperlinkColumn Name="productColumn"
                     Binding="{Binding Path=ProductUri}"
                     ContentBinding="{Binding Path=ProductName}"
                     Header="Product"
                     IsReadOnly="True"
                     Width="*">
<DataGridHyperlinkColumn.ElementStyle>
    <Style TargetType="TextBlock">
        <EventSetter Event="Hyperlink.Click" Handler="OnHyperlinkClick" />
    </Style>
</DataGridHyperlinkColumn.ElementStyle>

In the MSDN sample code for DataGrid, the EmailConverter is stripping out "mailto:" and the @ and everything after it, and only displaying the username portion of the mailto link. However, the underlying link, the href attribute in HTML terms, is unchanged. So, for example, the visible text might be "mjgreen," but the handler OnHyperlinkClick would receive a NavigateUri property of "mailto:[email protected]." The code to get NavigateUri, by the way, would look like this:

private void OnHyperlinkClick(object sender, RoutedEventArgs e)
{
    var destination = ((Hyperlink)e.OriginalSource).NavigateUri;
    Trace.WriteLine("Browsing to " + destination);

    using (Process browser = new Process())
    {
        browser.StartInfo = new ProcessStartInfo
            {
                FileName = destination.ToString(),
                UseShellExecute = true,
                ErrorDialog = true
            };
            browser.Start();
        }
}