0

I'm dynamically parsing data and adding text as Run, Hyperlinks and images as InlineUIContainer into a Windows Phone 8.0 RichTextBox. Somehow I can't manage that the images vertically align centered with the text.

Images are added like this:

        Paragraph paragraph = new Paragraph();
        richTextBox.Blocks.Add(paragraph);

        var img = new Image
        {
            Stretch = Stretch.Uniform,
            Source = imageSource,
            VerticalAlignment = VerticalAlignment.Center,
            Height = inlineImageSize,
        };

        paragraph.Inlines.Add(new InlineUIContainer {Child = img});

And text like that:

        Paragraph paragraph = new Paragraph();
        richTextBox.Blocks.Add(paragraph);
        paragraph.Inlines.Add(new Run { Text = text });

I tried to set a few values for alignment on the RichTextBox as well, but the text is never centered with the images. The text is always bottom aligned.

Any chance getting the inline images vertically centered with the inline text in the WP RichTextBox?

Rene Schulte
  • 2,962
  • 1
  • 19
  • 26
  • This might work as well, but it's quite bloated with all the custom controls surrounding http://stackoverflow.com/questions/5242508/silverlight-how-to-align-text-in-inlineuicontainer-content-with-outer-text-in-r – Rene Schulte Apr 24 '14 at 06:21

2 Answers2

1

I think what you are looking for is the BaselineAlignment Property. try the following :

Paragraph paragraph = new Paragraph();
    richTextBox.Blocks.Add(paragraph);

    var img = new Image
    {
        Stretch = Stretch.Uniform,
        Source = imageSource,
        BaselineAlignment = BaselineAlignment.Center,
        Height = inlineImageSize,
    };

    paragraph.Inlines.Add(new InlineUIContainer {Child = img});
Jarod Kientz
  • 131
  • 1
  • 9
  • This is for Windows Phone which does not have BaselineAlignment AFAIK or am I missing an include? – Rene Schulte Apr 23 '14 at 09:25
  • 1
    Sorry I didn't see the tag. It appears that Windows Phone 8, and Windows Phone 8.1 support BaselineAlignment, but Windows Phone 7 does not as noted on the [MSDN Library](http://msdn.microsoft.com/en-us/library/system.windows.documents.inline.baselinealignment(v=vs.110).aspx) – Jarod Kientz Apr 23 '14 at 14:35
  • I went and looked through all the properties of the image class in the [Windows Phone Developer Library](http://msdn.microsoft.com/en-us/library/windowsphone/develop/windows.ui.xaml.controls.image.aspx#properties) and I couldn't find anything like a BaselineAlignment or it equivilent. I guess in short... I don't have an answer for you if you're working with Windows Phone. Sorry. – Jarod Kientz Apr 23 '14 at 14:53
  • No worries. Thanks for taking the time. – Rene Schulte Apr 23 '14 at 18:14
1

Sorry for late reply. Try to set Margin for your Inline Images:

Paragraph paragraph = new Paragraph();
richTextBox.Blocks.Add(paragraph);

var img = new Image
{
    Stretch = Stretch.Uniform,
    Source = imageSource,
    Height = inlineImageSize,
    Margin = new Thickness(0,0,0,-5);
};

paragraph.Inlines.Add(new InlineUIContainer {Child = img});
fs_dm
  • 391
  • 3
  • 13