How is it possible to stuff a Grid inside a TextBl

2020-03-30 02:48发布

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?

标签: wpf textblock
1条回答
我命由我不由天
2楼-- · 2020-03-30 03:26

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.

查看更多
登录 后发表回答