开发者

WPF DataGrid DataGridHyperlinkColumn bound to Uri

No problem when binding to a property of string type ( "http://something.com" ). However , I seem to have se开发者_如何学编程en 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 ?


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:mjgreen@company.com." 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();
        }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜