Search code examples
windows-phone-8windows-phonestackpanel

How to wrap a group of TextBlocks inside StackPanel?


This is my code.

<StackPanel Orientation="Horizontal">
    <TextBlock Name="Link1" Text="this is link1"></TextBlock>
    <TextBlock Name="Text1" Text=" some text "></TextBlock>
    <TextBlock Name="Link2" Text="this is link2"></TextBlock>
    <TextBlock Name="Text2" Text=" some other text "></TextBlock>
    <TextBlock Name="Link3" Text="this is link3></TextBlock>
</StackPanel>

Now this StackPanel will go out of the screen. I want to wrap the contents of this to go into next line on screen end.

I am not using <Run> property of TextBlock for defining all this because I want to register Tap event for TextBlocks defined as Links in above code. Using Buttons and Hyperlinks is also not desired here. Also with Buttons I will be having the same issue of wrapping up the content.


Solution

  • I see three options here:

    1. Use a RichTextBox
    2. Hoist the WrapPanel from the Windows Phone Toolkit
    3. Write your own Panel

    I think #1 should be the preferred approach, assuming you can make it fit the requirements. A RichTextBox can contain plain text inline with Hyperlinks -- the latter does not expose a Tap event, but it does have Click which may suffice.

    Failing that, #2 should also be fairly straightforward, and may suffice if you can work with the limitation that the WrapPanel will treat the TextBlocks as atoms (ie, it won't wrap intra-TextBlock text).

    Approach #3 would allow more flexibility in theory, but it would take some amount of effort and expertise to implement. You also might face some hassles down the road when you might need to adapt your custom panel to new or unforeseen requirements.