简体   繁体   中英

Silverlight. How to align text in InlineUIContainer content with outer text in RichTextBox

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>

例

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.

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...

尝试使用Border.Margin属性..(尝试将其设置为“0,-5,0,-5”或其他一些数字)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM