3

The task: Make the text content of the InlineUIContainer to be inline with the outer text.

The standard behaviour of the InlineUIContainer content is when the bottom edge is inline with the outer text.

It is possible to shift the position of InlineUIContainer with RenderTransform, but the value of Y has to be chosen for each font type and size - not a perfect way.

<RichTextBox>

    <Paragraph>
        LLL
        <InlineUIContainer>
            <Border Background="LightGoldenrodYellow">
                <TextBlock Text="LLL"/>
            </Border>
        </InlineUIContainer>
        LLL
    </Paragraph>

    <Paragraph>
        LLL
        <InlineUIContainer>
            <Border Background="LightGoldenrodYellow">

                <Border.RenderTransform>
                    <TranslateTransform Y="5" />
                </Border.RenderTransform>

                <TextBlock Text="LLL"/>

            </Border>    
        </InlineUIContainer>
        LLL
    </Paragraph>

</RichTextBox>

Example

How to align the text in the InlineUIContainer content with the outer text in RichTextBox regardless of font type and size?

In WPF the property BaselineAlignment="Center" works fine.

But Silverlight seems lucking that functionality.

Community
  • 1
  • 1
Vadim Loboda
  • 2,431
  • 27
  • 44

2 Answers2

2

I fined i perfect way around (you can make a custom control from this):

First of all wrap your object into the Canvas...

<Paragraph>LLL
<InlineUIContainer>
    <Canvas x:Name="c" LayoutUpdated="c_LayoutUpdated">
        <Border Background="LightGoldenrodYellow">
            <TextBlock x:Name="t" FontSize="32"  Text="LLL"/>
        </Border>
    </Canvas>
</InlineUIContainer> LLL
</Paragraph>

And add LayoutUpdated event handler to Canvas

    private void c_LayoutUpdated(object sender, EventArgs e)
    {
        c.Width = t.DesiredSize.Width;
        c.Height = (t.DesiredSize.Height / 1.3d);         
    }

After pressing F5 you have to see a miracle :)

PS: Now text do as you wish... no mather what FontStyle and FontSize you use...

obenjiro
  • 3,665
  • 7
  • 44
  • 82
  • Thank you for your time and persistency! You are real wizard! It works like I dreamed. How have you found it? What is the magic coefficient "1.3d"? Where did it come from? – Vadim Loboda Mar 10 '11 at 07:29
  • 1.3d probably have to do with [dpi](http://en.wikipedia.org/wiki/Dots_per_inch) and the relation between 72 dpi and 96 dpi. – default Apr 08 '13 at 08:27
  • Thanks. This is very close to what I was trying to do. In my case, having the canvas be .75 of the height and reducing the font inside the textblock to .75 of the surrounding text in the RichTextBox gave me the desired effect of having a label embedded in my text. – froggythefrog Dec 29 '13 at 00:20
0

Try to play with Border.Margin property.. (try to set it to "0,-5,0,-5", or some other numbers)

obenjiro
  • 3,665
  • 7
  • 44
  • 82
  • By setting border margin property to "0,-5,0,-5" you saying "i don't want 'border' to fill 5 pixels from top and 5 pixels from bottom, leave this place for other controls, but still draw 'border'" – obenjiro Mar 09 '11 at 14:52
  • Margin property works as RenderTransform - one more way to do the same. If the font type or size changes it will require different margin value. The idea to use converter here is also not a solution - what a function to convert the font type and size to negative margin? – Vadim Loboda Mar 09 '11 at 15:45