Silverlight TextBox Watermark

Silverlight TextBox Watermark

by JBrooks 8. April 2011 07:45

The Silverlight TextBox does have a Watermark property, but Microsoft says “Do not use in a Silverlight 4 application” HERE.    So I looked into making one myself and it didn’t take much to do it. 

I just wanted the text box below to show the text “Search” in a grayed out way.


To do this it is just setting the forground color.

<TextBox Name="tbxSearch" Width="120" Text="Search" Foreground="#5A000000" 
    VerticalAlignment="Center" GotFocus="tbxSearch_GotFocus" />


I also wanted to reset the text to the normal color and to clear the text box when the user gave it focus.  Simple enough. If you notice above, the GotFocus event is wired up to the following method:

private void tbxSearch_GotFocus(object sender, RoutedEventArgs e)
    if (((SolidColorBrush)tbxSearch.Foreground).Color != Colors.Black)
        tbxSearch.Text = "";
        ((SolidColorBrush)tbxSearch.Foreground).Color = Colors.Black;

So that is it except that this should be made into a user control.