Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add text as content in Rectangle C# WPF

Tags:

c#

wpf

I have the following rectangle, which is filled with color. i want to add some text inside in middle of the box. please suggest some solution.

var rect1 = new Rectangle
{
     Stroke = new SolidColorBrush(Colors.Red),
     Fill = new SolidColorBrush(Colors.Black),
     Width = 150,
     Height = 100,
     VerticalAlignment = System.Windows.VerticalAlignment.Top,
     HorizontalAlignment = System.Windows.HorizontalAlignment.Left
 };
 Grid.SetRow(rect, 0);
 TGrid2.Children.Add(rect1);

Basically i want to make a drag and drop feature. where rect1 will be dragged over rect2; which will only have border color and transparent Fill-body and on drop replaces rect2 with dropped rect1. Thats why i made a rectangle and now trying to figure how to add text in rectangle as content in center, similar like a button.

like image 379
ADi Avatar asked Dec 29 '13 21:12

ADi


1 Answers

If you absolutely must use a rectangle, you could fake it!

<Grid>
    <Rectangle Fill="Red"/>
    <TextBlock Text="Hi there" Margin="5"/>
</Grid>
like image 105
Gusdor Avatar answered Sep 22 '22 03:09

Gusdor