In
this article, I will discuss how to create a TextBox control in WPF and also I
will discussing various properties of the TextBox control.
TextBox control
TextBox is the control which
accepts user input on a Form.
Properties - This control has the
following properties.

Figure 1.
Width - Width attributes of the TextBox
element represent the width of a TextBox.
Height - Height attributes of the
TextBox element represent the height of a TextBox.
Text - Text property of the TextBox
element sets the content of a TextBox.
Background - The Background attributes
set the background colors of text box.
Foreground -
The Foreground attributes set the foreground
colors of text box.
TextWrapping - The TextWrapping
attributes sets the wrapping of text.
Scrolling Text -
VerticalScrollBarVisibility and HorizontalScrollBarVisibility sets the vertical
and horizontal scroll bars visible.
IsReadOnly - IsReadOnly attribute to
true makes the text box non editable.
Creating a TextBox in XAML
<TextBox
Height="23"
HorizontalAlignment="Left"
Margin="10,10,0,0"
Name="TextBox1"
VerticalAlignment="Top"
Width="120"
Text=""
TextWrapping="WrapWithOverflow"
/>
The Height
and Width attributes sets the height and width of text box.
The form looks like this.

Figure 2.
Restricting Text Size of a
TextBox
The MaxLength
property of the TextBox sets the number of characters allowed to input in a text
box.
MaxLength="100"
Setting Background and Foreground Colors.
The Background and Foreground attributes set the background and foreground
colors of text box. The following code sets background color as red and
foreground color as yellow for the text box.
<TextBox
Height="23"
HorizontalAlignment="Left"
Margin="128,90,0,0"
Name="TextBox1"
VerticalAlignment="Top"
Width="180"
Text="Rohatash
Kumar"
TextWrapping="WrapWithOverflow"
Background="Crimson"
Foreground="Blue"
/>
The form looks like this.

Figure 3.
Wrapping and Scrolling Text:
The TextWrapping attributes sets the wrapping of text and
VerticalScrollBarVisibility and HorizontalScrollBarVisibility sets the vertical
and horizontal scroll bars visible.
<TextBox
Height="38"
HorizontalAlignment="Left"
Margin="128,90,0,0"
Name="TextBox1"
VerticalAlignment="Top"
Width="180"
Text="Rohatash
Kumar please enter something in the textbox"
TextWrapping="Wrap"
Background="Crimson"
Foreground="Blue"
HorizontalContentAlignment="Center"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Visible"
/>
The form looks like this.

Figure 4.
Setting the background image
<TextBox
Height="109"
HorizontalAlignment="Left"
Margin="128,90,0,0"
Name="TextBox1"
VerticalAlignment="Top"
Width="180"
Text="Rohatash
Kumar please enter something in the textbox"
TextWrapping="Wrap"
Foreground="Blue"
HorizontalContentAlignment="Center"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Visible">
<TextBox.Background>
<ImageBrush
ImageSource="/WpfApplication18;component/Images/flowers-image.jpg"
/>
</TextBox.Background>
</TextBox>
The form looks like this.

Figure 5.
Making TextBox as a Masking TextBox
While we take a input from the user it's very necessary that user should type
input in correct datatype, mean if integer value required then user should type
integer value and if string required then should type string. In WPF I am going
to show how we can bound user to do that, by masking textbox.
XAML code
<Window
x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Height="350"
Width="525">
<Grid>
<TextBlock
Height="23"
HorizontalAlignment="Left"
Margin="98,80,0,0"
Name="textBlock1"
Text="Enter
Value:"
VerticalAlignment="Top"
>
</TextBlock>
<TextBox
Height="23"
HorizontalAlignment="Left"
Margin="184,80,0,0"
Name="textBoxValue"
PreviewTextInput="textBoxValue_PreviewTextInput"
DataObject.Pasting="textBoxValue_Pasting"
VerticalAlignment="Top"
Width="120">
</TextBox>
</Grid>
</Window>
Add the following code with the event of TextBox.
Private Sub
textBoxValue_PreviewTextInput(ByVal sender
As Object,
ByVal e As
TextCompositionEventArgs)
e.Handled = Not
TextBoxTextAllowed(e.Text)
End Sub
Private Sub
textBoxValue_Pasting(ByVal sender
As Object,
ByVal e As
DataObjectPastingEventArgs)
If e.DataObject.GetDataPresent(GetType([String]))
Then
Dim Text1
As [String] =
DirectCast(e.DataObject.GetData(GetType([String])),
[String])
If Not
TextBoxTextAllowed(Text1) Then
e.CancelCommand()
End If
Else
e.CancelCommand()
End If
End Sub
Private Function
TextBoxTextAllowed(ByVal Text2
As [String]) As
[Boolean]
Return Array.TrueForAll(Of
[Char])(Text2.ToCharArray(), Function(c
As [Char]) [Char].IsDigit(c)
OrElse [Char].IsControl(c))
End Function
The form looks like this.

Figure 6.
Now run the application and test it.
If user try to type string value in this textbox
then he can't type. Permission only to type integer value.

Figure 7.