Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / WPF

A WatermarkTextBox in 3 lines of XAML

Rate me:
Please Sign up or sign in to vote.
4.65/5 (39 votes)
25 Jul 2008CPOL1 min read 181.6K   7.9K   91   20
Another example of how XAML can solve issues that most developers automatically assume will require "coding".

Introduction

Published examples demonstrating how to create a WatermarkTextBox in WPF, including the 'InfoTextBox' control in Kevin Moore's popular "Bag-O-Tricks" sample library always seem to involve a significant amount of code-behind, when a fully-functional version can be built with just three lines of XAML, leveraging WPF's built in BooleanToVisibilityConverter.

WaterMarkTextBoxDemo - Click to enlarge image

The complete project includes two variations on this functionality.

ScreenShot.jpg

This example stems from a discussion in a recent .NET User's Group meeting, in which several experienced WPF developers complained about the removal of the WatermarkTextBox control in the latest version of Silverlight. They all jumped to the conclusion (which I think is very common among developers) that supporting similar functionality would require "coding" a custom control of some kind.

Although this is a trivial example, I think the related question of when to use XAML vs. code (and, as possibly illustrated in my second example, knowing when you've begun to cross over into "hacking via markup"), will be one of several interesting XAML-related best practice debates on many real-world multi-developer projects -- especially when faced with a confusing mixture of seemingly random implementation choices around the third dev. cycle on a large code base.

Update: Microsoft has announced that it will add a "WaterMark" property to the TextBox in a future version of Silverlight, and has made the original WatermarkTextBox code available for download.

Other Projects by Andy L.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
I started out writing applications in C for a software development shop in Japan, did alot of work in C++/MFC, and some DirectX, at two Silicon Valley startups, and have been working with C# and Windows Forms ever since the release of .Net 1.0. Although I took a couple intro. to CS courses at CAL (Berkeley), my degree was actually in Asian Studies, and I learned to program "in the trenches". I was also the .Net evangelist at my most recent company, authoring internal white papers on .Net, sending out a weekly ".Net FYI" e-mail, conducting .Net training, and sweating the truth out of job candidates who claimed to have .Net experience (You'd be amazed at the number of Silicon Valley engineers who list "three years of C#" on their resumes, who are unable to explain how to hook up a simple event handler, or identify basic terms like "reflection", "attributes" -- or "Richter" and "Löwy").

Comments and Discussions

 
QuestionMight need a Rectangle? Pin
AvidFan28-Apr-17 1:52
AvidFan28-Apr-17 1:52 
QuestionVery sweet Pin
Sau0022-Nov-16 4:27
Sau0022-Nov-16 4:27 
QuestionMy Vote of 5 Pin
Member 113834634-May-16 20:38
Member 113834634-May-16 20:38 
GeneralMy vote of 5 Pin
Eugene Sadovoi7-Apr-16 10:07
Eugene Sadovoi7-Apr-16 10:07 
QuestionExcellent coding Pin
Anindya0026-Feb-16 3:36
Anindya0026-Feb-16 3:36 
GeneralMy vote of 2 Pin
Ashish Jain(Be Jovial)31-Aug-15 8:47
Ashish Jain(Be Jovial)31-Aug-15 8:47 
QuestionWatermark on top Pin
Jérôme Vuarand19-Aug-15 7:39
Jérôme Vuarand19-Aug-15 7:39 
QuestionPath=Text.IsEmpty - What is this? Pin
supernorb6-Sep-14 1:14
supernorb6-Sep-14 1:14 
AnswerRe: Path=Text.IsEmpty - What is this? Pin
Christopher Chappell19-Apr-15 6:10
Christopher Chappell19-Apr-15 6:10 
QuestionComboBox Pin
avinaxsht8-Jan-14 2:23
professionalavinaxsht8-Jan-14 2:23 
QuestionAmazing artical Pin
Priyank Thakkar21-Nov-13 4:39
Priyank Thakkar21-Nov-13 4:39 
GeneralMy vote of 5 Pin
iman khalil27-Aug-12 5:57
iman khalil27-Aug-12 5:57 
QuestionExcellent post Pin
patelsachin13-Dec-11 3:13
patelsachin13-Dec-11 3:13 
GeneralMy vote of 1 Pin
__Lj__28-Sep-10 4:12
__Lj__28-Sep-10 4:12 
GeneralHere's another no-code version, supports images too! Pin
awcullen214-Oct-08 4:36
awcullen214-Oct-08 4:36 
GeneralSimply great Pin
frinkfree24-Jun-08 5:09
frinkfree24-Jun-08 5:09 
This is a great example of simple xaml code replacing lines of code-behind. As far as excessive use of TextBlocks as mentioned by one commenter, I agree that this can be a resource issue, but most code-behind solutions that I have seen for this problem (including my own Sigh | :sigh: ) have used a paired TextBlock as well. The code download includes an expansion on the original idea (using multi-value converter) which I think is worth discussion in the main description.
<soapbox>
A lot of developers do not realize the power of xaml with converters. Combine that flexibility with a view model controller and you *almost* remove the need for often messy code-behind - it moves one step closer to the panacea of ui-code separation.
</soapbox>

Thanks for sharing this little bit of knowledge!
GeneralEasy solution, but think about efficiency Pin
Dusan Kocurek17-Jun-08 8:45
Dusan Kocurek17-Jun-08 8:45 
GeneralRe: Easy solution, but think about efficiency Pin
Josh Smith26-Jul-08 9:43
Josh Smith26-Jul-08 9:43 
GeneralPerfect Timing Pin
jackmos17-Jun-08 3:54
professionaljackmos17-Jun-08 3:54 
GeneralGood one Pin
Uroš Šmon15-Jun-08 22:48
professionalUroš Šmon15-Jun-08 22:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.