Frank Q.
Frank Q.

Reputation: 6592

How do I add text inside a shape in XAML

I am working on a Metro App using C++ and XAML. I want to create a polygon shape and add text inside it.

At first I thought of defining my own Controltemplate and apply it to Textblock but unfortunately it does not understand TargetType = "TextBlock".

Secondly, I thought of inheriting the Polygon class and see if I can do anything there but that class is sealed.

Any ideas on how to achieve this?

Upvotes: 7

Views: 9219

Answers (2)

Mehdi Khademloo
Mehdi Khademloo

Reputation: 2812

You can use something like this with ContentControl or so many other controls:

<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>

Upvotes: 0

Phil
Phil

Reputation: 42991

In WPF XAML you could do something simple like this:

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

To get text in the centre of a yellow ellipse.

I'm guessing something that simple will work on WinRT.

Upvotes: 16

Related Questions