C# default text for WPF textbox

Standard
Share

I’m working on a project developing an application for a touch-screen interface.  As such, all of my screen elements are BIG, to accommodate navigation via touch.  It occurred to me that I could save space by eliminating labels for textboxes.  Of course, I don’t really want to have a bunch of textboxes with no way of knowing which textbox is for which bit of data.  I wanted a way to have the textbox display default text when it’s empty, but to allow for otherwise normal input.  I chose to implement both the OnTextChanged and PreviewTextInput methods to accommodate this functionality.

private void Search_TextChanged(object sender, TextChangedEventArgs e)
{
    if (((TextBox)sender).Text.Length == 0)
        ((TextBox)sender).Text = ((TextBox)sender).Tag.ToString();
}

In the OnTextChanged event handler, we’ll check to see if the length of the textbox contents is 0.  If it is, we simply set the default text to whatever we want.

private void Search_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
    if (((TextBox)sender).Text == ((TextBox)sender).Tag.ToString())
    {
        ((TextBox)sender).Text = e.Text;
        e.Handled = true;
        ((TextBox)sender).SelectionStart = 1;
    }
}

In the PreviewTextInput event handler, we check to see if the existing textbox contents matches the default text.  If it does, we simply replace the textbox contents with the depressed key.

I’ve made use of the textbox object’s “tag” property to set the default text.  This way, I can keep my event handler generic, and tie all of my textboxes to these two event handlers.

Now, assuming your textbox has a tag set to “default text” and its PreviewTextInput and TextChanged event handlers are tied to the code above, you should observe the behavior of the textbox displaying “default text” unless the box has focus and the user begins to type.  As soon as text is input – nay, BEFORE text is input, our PreviewTextInput method executes.

  1. The user presses a key or input is otherwise generated
  2. PreviewTextInput executes
  3. If not otherwise handled, the key press bubbles to the surface

You’ll also note the

((TextBox)sender).SelectionStart = 1;

line.  Without this line, I observed odd behavior with regards to the cursor placement appearing before the newly typed character, placing subsequent characters before the initial character.  For example, typing “abc” in the textbox would yield “bca” in the textbox.  To alleviate this, after we handle the character input, we place the cursor to the end of the textbox, allowing subsequent character input to appear after the initial character input.

UPDATE
It has come to my attention that the Extended WPF Toolkit (which I discussed in my article on the color picker) contains a control titled WatermarkTextBox. This control provides the behavior for which I was looking – default text when the control has no value.

Its usage is quite simple. First, install the Extended WPF Toolkit if you haven’t already. The simplest method is to install the NuGet package via Visual Studio’s Package Manager console.

Next, in the namespace declaration of your XAML, declare a reference to the toolkit:

xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"

Then simply declare an instance of the WatermarkTextBox. The pertinent property here is “Watermark”, which can be set in XAML or bound to a property just like any other TextBox’s Text property:

<xctk:WatermarkTextBox Watermark="Search/Add new brand" Text="{Binding FilterText, UpdateSourceTrigger=PropertyChanged}" />

I advise anyone needing a textbox with default text in it to use the Extended WPF Toolkit’s WatermarkTextBox control.

One thought on “C# default text for WPF textbox

Leave a Reply

Your email address will not be published. Required fields are marked *