Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add text to the shapes in WPF

Tags:

c#

.net

wpf

I am drawing Circle on an WPF window. The problem is that I am unable add Text to the Circle. The code is given below:

public Graphics()
{
    InitializeComponent();

    StackPanel myStackPanel = new StackPanel();
    Ellipse myel = new Ellipse();
    SolidColorBrush mscb = new SolidColorBrush();
    mscb.Color = Color.FromArgb(255, 255, 0, 0);
    myel.Fill = mscb;
    myel.StrokeThickness = 2;
    myel.Stroke = Brushes.Black;
    myel.Width = 100;
    myel.Height = 100;
    //string str = "hello";
    myStackPanel.Children.Add(myel);
    this.Content = myStackPanel;
}

Please help me in this regard.

like image 479
Muhammad Umer Avatar asked May 05 '13 16:05

Muhammad Umer


1 Answers

Shapes are simply shapes, if you want to add text then add both the shape and a TextBlock with your text to a common container which lays them on top of each other, e.g. a Grid without rows or columns.

In XAML:

<Grid>
    <Ellipse Width="100" .../>
    <TextBlock Text="Lorem Ipsum"/>
</Grid>

C#

var grid = new Grid();
grid.Children.Add(new Ellipse { Width = 100, ... });
grid.Children.Add(new TextBlock { Text = "Lorem Ipsum" });
like image 200
H.B. Avatar answered Sep 27 '22 20:09

H.B.