Click here to Skip to main content
6,822,123 members and growing! (18,362 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Presentation Foundation » XAML     Beginner License: The Code Project Open License (CPOL)

A WatermarkTextBox in 3 lines of XAML

By AndyL2

Another example of how XAML can solve issues that most developers automatically assume will require "coding".
C#3.0.NET3.0, .NET3.5, XAML, WPF, Dev, Design
Revision:2 (See All)
Posted:15 Jun 2008
Updated:25 Jul 2008
Views:16,460
Bookmarked:39 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
16 votes for this article.
Popularity: 4.91 Rating: 4.08 out of 5
1 vote, 6.3%
1
1 vote, 6.3%
2
2 votes, 12.5%
3
3 votes, 18.8%
4
9 votes, 56.3%
5

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)

About the Author

AndyL2


Member
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").
Location: United States United States

Other popular Windows Presentation Foundation articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
GeneralHere's another no-code version, supports images too! Pinmemberawcullen25:36 14 Oct '08  
GeneralSimply great Pinmemberparxs6:09 24 Jun '08  
GeneralEasy solution, but think about efficiency PinmemberDusan Kocurek9:45 17 Jun '08  
GeneralRe: Easy solution, but think about efficiency PinmvpJosh Smith10:43 26 Jul '08  
GeneralPerfect Timing Pinmemberjackmos4:54 17 Jun '08  
GeneralGood one PinmemberUro23:48 15 Jun '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 25 Jul 2008
Editor: Deeksha Shenoy
Copyright 2008 by AndyL2
Everything else Copyright © CodeProject, 1999-2010
Web21 | Advertise on the Code Project