Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the text of a WPF Hyperlink via data binding?

It looks strange, but it works. We do it in about 20 different places in our app. Hyperlink implicitly constructs a <Run/> if you put text in its "content", but in .NET 3.5 <Run/> won't let you bind to it, so you've got to explicitly use a TextBlock.

<TextBlock>
    <Hyperlink Command="local:MyCommands.ViewDetails" CommandParameter="{Binding}">
        <TextBlock Text="{Binding Path=Name}"/>
    </Hyperlink>
</TextBlock>

Update: Note that as of .NET 4.0 the Run.Text property can now be bound:

<Run Text="{Binding Path=Name}" />

This worked for me in a "Page".

<TextBlock>
    <Hyperlink NavigateUri="{Binding Path}">
        <TextBlock Text="{Binding Path=Path}" />
    </Hyperlink>
</TextBlock>

On Windows Store app (and Windows Phone 8.1 RT app) above example does not work, use HyperlinkButton and bind Content and NavigateUri properties as ususal.