如何在 TextBlock 中填充 Grid? [英] How is it possible to stuff a Grid inside a TextBlock?

查看:31
本文介绍了如何在 TextBlock 中填充 Grid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一天,我遇到了以下 xaml 并吓坏了:

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>

换句话说……如何将 Grid 放入 TextBlock 中?

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

推荐答案

简单的答案是您可以通过两种方式驱动 TextBlock ......通过 Text 属性和通过 Inlines 集合.

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

在本例中,您使用的是 Inlines 集合.

In this case, you are using the Inlines collection.

TextBlock(通过 TextElement 上的 IAddChild.AddChild 方法)足够聪明,可以将该 Grid 包装到一个 InlineUIContainer 中……当然,这是一个 Inline.

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

换句话说,上面的 xaml ... 是一样的:

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.

这篇关于如何在 TextBlock 中填充 Grid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆