WPF TextBox

Jose Leon

The textbox is one the most popular controls in WPF. It allows user to input data or text into your application. The information they put in here is basically plain text that you can later manipulate using code. Below you can find a simple example using XAML code:

For changing the text inside the textbox, you can use the Text property and type the label you want to show. In the sample below, we're pre-filling the text with the welcome word:

Numeric textbox

If need to make a textbox just numeric, we need to add some simple code in the application that will validate the characters on the textbox and just let numbers to go through. Below you can check the XAML code:

This is the C# code:

namespace WpfApplication12{    ///     /// Interaction logic for MainWindow.xaml    ///     public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();        }        private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)        {            Regex regex = new Regex("[^0-9]+");            e.Handled = regex.IsMatch(e.Text);        }    }}

In the code above, we're validating that text contains numbers using regular expressions. If the text contains only numbers, the IsMatch method returns true allowing then the event to be handled. If not, the event is cancelled and nothing is changed on the textbox.


All posts

  • licensing
  • analytics

Easy .NET Protection

Purchase template