Question

I have a textblock and an image inside a stackpanel like this:

<StackPanel Height="Auto" Name=stackPanel" Width="Auto" Orientation="Horizontal" >
    <TextBlock Height="Auto" Name="textBlock" Width="Auto" TextWrapping="Wrap" MaxWidth="168" />
    <Image Margin="10,10,0,0" Name="image" />
</StackPanel>

So the image is next to the textblock. If the text of textblock is to long, the text will be wrapped. Now I want the image to drop a row too, so the image stays next to the last word of the textblock.

My idea was to do this with an if-statement:

if (textblockBlock.ActualHeight > 35)
    {
        // change margin
    }

But this doesn't work, as the actual height of the textblock (appearently) doesn't change when the text is wrapped...

Could someone help me out with this?

Was it helpful?

Solution

Check for text length, if length is greater than your specified value then change margin

if (textblock.Text.Length>40)
    { 
     //change margin
    }

OTHER TIPS

If you want the image to always be at the "bottom" of the last row, just set the VerticalAlignment property to Bottom:

   <Image Margin="10,10,0,0" Name="image" VerticalAlignment="Bottom" />

This will cause the Image to always be aligned to the bottom, regardless of how many lines of text are used by the TextBlock.

Also, the ActualHeight property should be set, but depending on when you've attempted to read the value, it may not be available.

You could also use a RichTextBox (which on the Phone is actually read-only):

<RichTextBox MaxWidth="168" IsReadOnly="True">
    <Paragraph>
        <Run Text="This is the text from a question on Stack Overflow."/>
        <InlineUIContainer>
            <Image Source="Assets/circle.png" Width="20" Height="20" />                        
        </InlineUIContainer>
    </Paragraph>
 </RichTextBox>

With that, you can embed an Image directly in the content.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top