Adam Sills
Adam is a senior consultant at Catapult Systems and blogs about development on the .NET Framework, with a focus on Silverlight and WPF application development.

TextBox.Watermark in Silverlight 4   

Tags: Silverlight
Technorati Tags:

While browsing MSDN documentation, you may come across this gem: TextBox.Watermark.

“Awesome! I’ve been wanting a built-in way to do watermarking on my text boxes! This is great, let me go ahead and set that in XAML!”

<TextBox Watermark="This is my watermark" Margin="20"></TextBox>

Unfortunately, if you run this you won’t get what you expect:


And the detail:


What is this? Well, look at the MSDN documentation closely:


That’s right. It’s supported in Silverlight 4, but it also says “Do not use in a Silverlight 4 application”. If you do use it, you receive a System.NotImplemented exception. To verify, here is the code for the property decompiled via Reflector:

public object Watermark
        return base.GetValue(WatermarkProperty);
        base.SetValue(WatermarkProperty, value);

There it is – it throws an exception any time it’s not in design mode. This makes no sense right? Why would Microsoft do this?

Unfortunately I haven’t found any definitive answer yet, however if I had to guess it’s because Microsoft is planning on implementing a Watermark behavior on the TextBox control in a future version (perhaps v5) and wanted to effectively reserve this property so third party control creators don’t subclass TextBox and create their own Watermark property. I know of at least one control vendor, ComponentOne, who has a control that inherits from TextBox and provides a Watermark property. To me, it seems this is Microsoft’s way of discouraging people from using this property name on their own TextBox subclasses.

Posted by  Adam Sills  on  10/18/2010
0  Comments  |  Trackback Url  | 0  Links to this post | Bookmark this post with:        

Links to this post


Name *:

CAPTCHA Image Validation