ADi
ADi

Reputation: 219

How to add text as content in Rectangle 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.

Upvotes: 11

Views: 46833

Answers (2)

Gusdor
Gusdor

Reputation: 14334

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

<Grid>
    <Rectangle Fill="Red"/>
    <TextBlock Text="Hi there" Margin="5"/>
</Grid>

Upvotes: 23

JYL
JYL

Reputation: 8319

Replace the Rectangle by a Grid or a Border and put a TextBlock inside.

Upvotes: 7

Related Questions