How to define the skin for HyperlinkButton with Content / Text databound?


I am binding a collection (rss feed) into a list box such as this:

<ListBox Margin="0,0,-12,0" ItemsSource="{Binding Items}">
            <StackPanel Margin="0,0,0,17" Width="432">
                <HyperlinkButton Content={Binding Title} NavigateUri="{Binding Link}" />
                <TextBlock Text="{Binding Description}" />

This works great - the data displays correctly etc. But now when I changed it to use text wrapping, the title is not displaying anymore.

Here is the problematic code.

<ListBox Margin="0,0,-12,0" ItemsSource="{Binding Items}">
            <StackPanel Margin="0,0,0,17" Width="432">
                <HyperlinkButton NavigateUri="{Binding Link}">
                    <TextBlock Text="{Binding Title}" TextWrapping="Wrap" />
                <TextBlock Text="{Binding Description}" TextWrapping="Wrap" />

I don't think it's the "TextWrapping" attribute that is causing the issue, since I tried without it and it still did not work. So my question is how do you get something like this to work? I just want to display a hyperlink with wrapped bound text. It looks like a fairly simple thing to do - but yet so hard. Help?

I was having the exact same problem and I couldn't understand why it wasn't working until I ran across this blog post by Mister Goodcat. In his post he said that because of some optimizations made for the WP7 in Silverlight the basic functionality that works in normal Silverlight does not work correctly for WP7 Silverlight. Instead of using he suggests modifying the Style instead.

The easiest way to edit the default templates still is to use Expression Blend to make a copy of it and work from there. When you do that you'll see that the template really only has a text block to show the content. That is why using another UI element as content doesn't work for the hyperlink button on WP7. If you only want to make the text wrap, it's sufficient to change the TextWrapping property on that text block.

<Style x:Key="HyperlinkButtonWrappingStyle"
    <Setter Property="Foreground"
            Value="{StaticResource PhoneForegroundBrush}" />
    <Setter Property="Background"
            Value="Transparent" />
    <Setter Property="FontSize"
            Value="{StaticResource PhoneFontSizeMedium}" />
    <Setter Property="Padding"
            Value="0" />
    <Setter Property="Template">
            <ControlTemplate TargetType="HyperlinkButton">
                <Border Background="Transparent">
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal" />
                            <VisualState x:Name="MouseOver" />
                            <VisualState x:Name="Pressed">
                                    <DoubleAnimation Duration="0"
                                                        Storyboard.TargetName="TextElement" />
                            <VisualState x:Name="Disabled">
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground"
                                        <DiscreteObjectKeyFrame KeyTime="0"
                                                                Value="{StaticResource PhoneDisabledBrush}" />
                    <Border Background="{TemplateBinding Background}"
                            Margin="{StaticResource PhoneHorizontalMargin}"
                            Padding="{TemplateBinding Padding}">
                        <TextBlock x:Name="TextElement"
                                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                    Text="{TemplateBinding Content}"
                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                    TextWrapping="Wrap" />

I suggest reading his blog post for more information & help.