Search code examples
c#xamlwindows-phone-8windows-phone

Create HyperlinkButton with text wrapping in code behind (Windows Phone 8)


I have the following xaml snippet that I need to create in code behind. Is this possible to do?

<HyperlinkButton Name="hyperlinkButton1" NavigateUri="http://www.google.com" Margin="0" Padding="0">
     <HyperlinkButton.Template>
           <ControlTemplate TargetType="HyperlinkButton">
                <Border BorderThickness="0" Padding="0" Margin="0">
                    <StackPanel Margin="0">
                          <Image Source="/Assets/Logo.png" Margin="0"/>
                          <TextBlock VerticalAlignment="Center" Text="Go to View.xaml" Foreground="Green"/>
                    </StackPanel>
                </Border>
           </ControlTemplate>
     </HyperlinkButton.Template>
</HyperlinkButton>

Solution

  • You can put the xaml above into a long string (start with @" to have a multi-line string in C#) and then call XamlReader.Load( myXamlString ).

    Alternatively, you can create the hyperlink button and set its template in code-behind. Even in that case, however, the general advice is to use XamlReader to create the template from a string.