How to create a TextBox in WPF and C#

The TextBox element represents a WPF TextBox control in XAML.

 

<TextBox/>

 

The Width and Height attributes of the TextBox element represent the width and the height of a TextBox. The Text property of the TextBox element sets the content of a TextBox. The Name attribute represents the name of the control, which is a unique identifier of a control.

 

The code snippet in Listing 1 creates a TextBox control and sets the name, height, width, and content of a TextBox control.

 

<TextBox Name="TextBox1" Height="30" Width="200"

        Text="Hello! I am a TextBox.">           

</TextBox>

Listing 1


We can place a TextBox control where we want by using the Margin, VerticalAlignment and HorizontalAlignment attributes that sets the margin, vertical alignment, and horizontal alignment of a control.

The code snippet in Listing 2 sets the position of the TextBox control in the left top corner of the page.

<TextBox Name="TextBox1" Height="30" Width="200"

        Text="Hello! I am a TextBox."

        Margin="10,10,0,0" VerticalAlignment="Top"

        HorizontalAlignment="Left">           

</TextBox>

Listing 2