---
title: "Textbox control in WPF"  
description: "XAML  element represents Textbox control in WPF. Whenever you want to provide a functionality to accept new values from user then we use concept of Te"  
author: "Anonymous User"  
published: 2011-03-30  
updated: 2020-03-04  
canonical: https://www.mindstick.com/articles/523/textbox-control-in-wpf  
category: "wpf"  
tags: ["wpf"]  
reading_time: 3 minutes  

---

# Textbox control in WPF

XAML <Textbox /> element represents [Textbox control](https://www.mindstick.com/articles/320/textbox-control-in-vb-dot-net) in WPF. Whenever you want to provide a [functionality](https://www.mindstick.com/blog/136/using-watermark-functionality-in-textbox-by-jquery) to accept new values from user then we use concept of Textbox control. In this article I will explain you some basic [properties](https://yourviews.mindstick.com/view/81845/now-it-s-possible-to-predict-properties-of-any-molecule) of textbox control such as [background](https://www.mindstick.com/articles/65/how-to-change-background-color-of-tab-control-in-c-sharp), foreground, setting size and positions, [wrapping](https://www.mindstick.com/interview/399/q-why-should-i-bother-wrapping-items-in-a-comboboxitem), scrolling and [validation](https://www.mindstick.com/articles/43/validation-controls-in-asp-dot-net) to the textbox control.\

##### Creating a Textbox

A XAML code snippet which represents a simple textbox.

\

```
<TextBox x:Name="txtSampleTextBox" Width="200" Height="30" />
```

Here x:Name [property](https://www.mindstick.com/articles/198559/an-ownership-of-property-in-hua-hin) is used to provide a unique identifier to your WPF control. Width property represents width of the control while height properties represent height of the control.

##### Output of the above code snippet is

![Textbox control in WPF](https://www.mindstick.com/mindstickarticle/7bf19653-3eb2-41cf-b456-acbef4972630/images/66a32645-114a-4db6-a6ef-5498667164c0.png)

##### Setting Background and Foreground colors

The following code snippet represents Background and Foreground color of a textbox control.

```
<TextBox x:Name="txtSampleTextBox" Width="200" Background="#aabb11" Foreground="#ff0000" Height="30" />
```

In above code snippet for providing back color and fore color I had chosen hexadecimal color codes. # means the code that is represented is in hexadecimal format. Every color is made of three combinations. First two characters represents red whose maximum value is FF and minimum value 00 next represents Green whose maximum values FF and Minimum values 00 and last one represents Green whose maximum value is FF and minimum value is 00.

##### The output the above code snippet is as follows

![Textbox control in WPF](https://www.mindstick.com/mindstickarticle/7bf19653-3eb2-41cf-b456-acbef4972630/images/300a8cea-8b4b-4001-a5cf-a1160978c0c5.png)

##### Setting font style in textbox control

The following code snippet represents that how to apply font on textbox control.

\

```
 <TextBox x:Name="txtSampleTextBox" Width="200" FontFamily="Lucida Console" FontSize="14" FontStretch="Condensed" FontStyle="Italic" Height="30" />
```

Here FontFamily properties represents family of the font, FontSize represents size of the font and FontStyle represents style of the font.

##### The output of the following code snippet is

![Textbox control in WPF](https://www.mindstick.com/mindstickarticle/7bf19653-3eb2-41cf-b456-acbef4972630/images/199ce651-f078-4c6d-a065-1a9872f7283b.png)

##### Wrapping and Scrolling in textbox control

The following code snippet represents wrapping and scrolling feature of textbox control.

```
<TextBox x:Name="txtSampleTextBox" Width="200" FontFamily="Lucida Console" FontSize="14" FontStretch="Condensed" FontStyle="Italic" Height="60" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" />
```

\

Here in following code snippet I used two properties one is TextWrapping which sets to wrap for implementing Wrapping functionality and VerticalScrollBarVisiblity which sets to be auto that means when needed scroll bar appears.

##### The output of the following code snippet is as follows

![Textbox control in WPF](https://www.mindstick.com/mindstickarticle/7bf19653-3eb2-41cf-b456-acbef4972630/images/420dac6d-413c-4bc6-9b2c-0f6aad000ebf.png)

##### Restricting text in a textbox control

We can implement some restriction in textbox control by implementing certain properties such as maximum height, maximum width, maximum number of lines, minimum number of lines, [maximum length](https://answers.mindstick.com/qa/40973/what-is-the-maximum-length-of-a-url-in-different-browsers) and [minimum length](https://www.mindstick.com/forum/95357/how-to-use-summernote-minimum-length-validation) etc.

---

Original Source: https://www.mindstick.com/articles/523/textbox-control-in-wpf

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
