Home > DeveloperSection > Beginner > Textbox control in WPF

Textbox control in WPF


WPF WPF 
Ratings:
0 Comment(s)
 5727  View(s)
Rate this:

Textbox control in WPF

XAML <Textbox /> element represents Textbox control in WPF. Whenever you want to provide a functionality to accept new values from user then we use concept of Textbox control. In this article I will explain you some basic properties of textbox control such as background, foreground, setting size and positions, wrapping, scrolling and validation 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 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

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

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

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

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 and minimum length etc.

 


Don't want to miss updates? Please click the below button!

Follow MindStick