How do I add text inside a shape in XAML
Asked Answered
G

2

7

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?

Goosy answered 13/3, 2012 at 4:23 Comment(0)
F
16

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.

Fungicide answered 13/3, 2012 at 7:10 Comment(0)
D
0

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>
Delitescent answered 8/7, 2016 at 0:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.