Wednesday, January 19, 2011

TextWrapping for HyperlinkButton on Windows Phone 7 WP7

Thanks to MisterGoodcat!

From:

This is the magic style:
 <Style x:Key="HyperlinkButtonWrappingStyle"         TargetType="HyperlinkButton">     <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">         <Setter.Value>             <ControlTemplate TargetType="HyperlinkButton">                 <Border Background="Transparent">                     <VisualStateManager.VisualStateGroups>                         <VisualStateGroup x:Name="CommonStates">                             <VisualState x:Name="Normal" />                             <VisualState x:Name="MouseOver" />                             <VisualState x:Name="Pressed">                                 <Storyboard>                                     <DoubleAnimation Duration="0"                                                         To="0.5"                                                         Storyboard.TargetProperty="Opacity"                                                         Storyboard.TargetName="TextElement" />                                 </Storyboard>                             </VisualState>                             <VisualState x:Name="Disabled">                                 <Storyboard>                                     <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground"                                                                     Storyboard.TargetName="TextElement">                                         <DiscreteObjectKeyFrame KeyTime="0"                                                                 Value="{StaticResource PhoneDisabledBrush}" />                                     </ObjectAnimationUsingKeyFrames>                                 </Storyboard>                             </VisualState>                         </VisualStateGroup>                     </VisualStateManager.VisualStateGroups>                     <Border Background="{TemplateBinding Background}"                             Margin="{StaticResource PhoneHorizontalMargin}"                             Padding="{TemplateBinding Padding}">                         <TextBlock x:Name="TextElement"                                     HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"                                     Text="{TemplateBinding Content}"                                     TextDecorations="Underline"                                     VerticalAlignment="{TemplateBinding VerticalContentAlignment}"                                     TextWrapping="Wrap" />                     </Border>                 </Border>             </ControlTemplate>         </Setter.Value>     </Setter> </Style>

1 comment: