tags:

views:

29

answers:

1

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?

+1  A: 

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.

cplotts