TextBox and MaskedTextBox controls in WPF
TextBox:
Namespace : System.Windows.Controls.TextBox
The Text property of the TextBox element sets the content of a TextBox.
The Name attribute represents the name of the control.
Designer Code:
<!--Window1.XAML-->
<TextBox Name="Sample" Height="30" Width="180"
TextChanged="Sample_TextChanged" >Text Here!</TextBox>
Code Behind:
// Window1.xaml.cs
private void Sample_TextChanged(object sender, TextChangedEventArgs e)
{
MessageBox.Show(Sample.Text);
}
TextBox Formatting :
Use the BorderBrush/brush property of textbox to draw border for textcontrol.
// Window1.xaml.cs
<TextBox Name="Sample" Height="30" Width="180"
TextChanged="Sample_TextChanged" Text=" Text Here!"
Margin="10,10,0,0" VerticalAlignment="Top"
HorizontalAlignment="Left">
<TextBox.BorderBrush>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1" >
<GradientStop Color="Red" Offset="0" />
<GradientStop Color="Blue" Offset="1.0" />
</LinearGradientBrush>
</TextBox.BorderBrush>
</TextBox>
TextBox Background and Foreground Formatting :
Use the Background/Foreground to set the back fore colors for text box.
TextBox.Background ="Orange" TextBox.Foreground ="Blue"
FontSize="16" FontFamily="Verdana" FontWeight="Normal" For fonts
<TextBox.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1" >
<GradientStop Color="RosyBrown" Offset="0.1" />
<GradientStop Color="Blue" Offset="0.25" />
<GradientStop Color="Bisque" Offset="0.75" />
<GradientStop Color="Orange" Offset="1.0" />
</LinearGradientBrush>
</TextBox.Background>
Image as Background of a TextBox :
<TextBox.Background>
<ImageBrush ImageSource="Win2wpf.JPG" />
</TextBox.Background>
Other properties of TextBox :
<TextBox x:Name="Sample1" Margin="10,10,50,0"
Width="200" Height="30"
HorizontalScrollBarVisibility="Visible"<!--For HScroll-->
VerticalScrollBarVisibility="Visible"<!--For VScroll-->
TextWrapping="Wrap" <!--For Wrapping Text-->
TextAlignment="Right"<!--For Text alingment in Textbox-->
MaxLength="500"<!--For maximum length of chars-->
IsReadOnly="False" <!--For non editable-->
AcceptsReturn="True" > <!--For accepting return-->
</TextBox>
Spell checking functionality of TextBox :
Supports only four languages(English, Spanish, German and French), have to specify the language.
<TextBox SpellCheck.IsEnabled="True" Language="en-US" />
Auto Validating TextBox Control :
Create a user control validatingTextBox, copy following code in designer:
<UserControl x:Class="WPFTutorial.validatingTextBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="30" d:DesignWidth="131">
<Grid Height="24" Width="122">
<TextBox Height="23" HorizontalAlignment="Left" Name="validatingTextBox1"
PreviewTextInput="validatingTextBox1_PreviewTextInput"
DataObject.Pasting="validatingTextBox1_Paste"
VerticalAlignment="Top" Width="120" />
</Grid>
</UserControl>
Add the following code in the code behind of usercontrol:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPFTutorial
{
/// <summary>
/// Interaction logic for MaskedTextBox.xaml
/// </summary>
public partial class validatingTextBox : UserControl
{
private bool stringValidation;
//START-- CODE IS FOR ACCESSOR BEHAVIOUR
public bool StringValidation
{
get { return this.stringValidation; }
set { stringValidation = value; }
}
public validatingTextBox()
{
InitializeComponent();
}
private void validatingTextBox1_Paste(object sender,
DataObjectPastingEventArgs e)
{
if (e.DataObject.GetDataPresent(typeof(String)))
{
String val = (String)e.DataObject.GetData(typeof(String));
if (!stringValidation)
{
if (!ValidateText(val)) e.CancelCommand();
}
else
{
if (!ValidateNum(val)) e.CancelCommand();
}
}
else e.CancelCommand();
}
private Boolean ValidateText(String str)
{
return Array.TrueForAll(str.ToCharArray(),
delegate(Char c) { return Char.IsDigit(c) || Char.IsControl(c);});
}
private Boolean ValidateNum(String str)
{
return Array.TrueForAll(str.ToCharArray(),
delegate(Char c) { return Char.IsLetter(c) || Char.IsControl(c);});
}
private void validatingTextBox1_PreviewTextInput(object sender,
TextCompositionEventArgs e)
{
if (!stringValidation)
{
e.Handled = !ValidateText(e.Text);
}
else
{
e.Handled = !ValidateNum(e.Text);
}
}
}
}
To use validatingTextBox usercontrol in application Add following code:
<my:validatingTextBox HorizontalAlignment="Left" Margin="67,53,0,0"
x:Name="validatingTextBox1" VerticalAlignment="Top"
StringValidation="True" />
Here
StringValidation value to true validates for numeric values and false validates for letters.
MaskedTextBox:
No direct equivalent in WPF.Use Windows forms host for this or use the control provided in code plex
You may be able to validate input using the System.ComponentModel.MaskedTextProvider.Add references to WindowsFormsIntegration.dll from program files\reference assemblies and System.Windows.Forms from the .net tab.
WindowsFormsHost integration for Masked TextBox:
<Window x:Class="WindowsApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WindowsApplication1"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
>
<Grid>
<WindowsFormsHost>
<wf:MaskedTextBox></wf:MaskedTextBox>
</WindowsFormsHost>
</Grid>
</Window>
Masked TextBox control provided in code plex:
Link:http://wpftoolkit.codeplex.com/wikipage?title=MaskedTextBox
Code:
<toolkit:MaskedTextBox Mask="(000) 000-0000" Value="(555) 123-4567"
IncludeLiterals="True" />