简体   繁体   中英

How is it possible to stuff a Grid inside a TextBlock?

The other day I ran into the following xaml and I freaked out:

<Grid x:Name="LayoutRoot">
    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">
        <Grid>
            <Rectangle Fill="AliceBlue" Width="25" Height="25"/>
        </Grid>
    </TextBlock>
</Grid>

In other words ... how is it possible to put a Grid inside a TextBlock?

The simple answer is that you can drive TextBlock in two ways ... through the Text property and through the Inlines collection.

In this case, you are using the Inlines collection.

TextBlock (via the IAddChild.AddChild method on TextElement) is smart enough to wrap that Grid into an InlineUIContainer ... which is, of course, an Inline.

In other words, the above xaml ... is the same as:

<Grid x:Name="LayoutRoot">
    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">
        <InlineUIContainer>
            <Grid>
                <Rectangle Fill="AliceBlue" Width="25" Height="25"/>
            </Grid>
        </InlineUIContainer>
    </TextBlock>
</Grid>

Hope this helps someone to avoid the freakout I had. Heh, heh. Well, at least, I hope it calms them down with an understanding of how it works.

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