如何在XAML中的形状内添加文本

问题描述 投票:7回答:2

我正在使用C ++和XAML开发Metro App。我想创建一个多边形形状并在其中添加文本。

起初我想到定义我自己的Controltemplate并将其应用于Textblock但不幸的是它不理解TargetType =“TextBlock”。

其次,我想继承Polygon类,看看我是否可以做任何事情,但那个类是密封的。

关于如何实现这一点的任何想法?

xaml microsoft-metro
2个回答
15
投票

在WPF XAML中,您可以执行以下简单操作:

<Grid Width="60" Height="100">
    <Ellipse Fill="Yellow"/>
    <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="Hello"/>
</Grid>

在黄色椭圆的中心获取文本。

我猜这个简单的东西可以在WinRT上运行。


0
投票

你可以使用ContentControl或许多其他控件这样的东西:

<ContentControl Width="200" Height="100" Content="Something">
    <ContentControl.Template>
        <ControlTemplate>
            <Grid>
                <Ellipse Fill="Red"/>
                <TextBlock Text="{Binding Content,RelativeSource={RelativeSource FindAncestor,AncestorType=ContentControl}}" 
                            TextWrapping="Wrap"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Center"/>
            </Grid>
        </ControlTemplate>
    </ContentControl.Template>
</ContentControl>
© www.soinside.com 2019 - 2024. All rights reserved.