The watermark function of textbox is provided in silverlight2. However, this function is deleted in later versions. For more information about the watermark function in silverlight2, see this article.ArticleLearn Silverlight 2 series (2): basic controls step by step. Then I want to write one watermark by myself.
Below is a textbox with a watermark written by myself.
1. Create a class mytextbox that inherits the textbox. 2. Add a watermarktext attribute to the mytextbox class to save the watermark.
In addition to adding an attribute, you also need to add some global variables that save the attributes different from the normal state.
//Watermark statusPrivateBrush _ redcolor =NewSolidcolorbrush (colors. Red );Private Double_ Halfopacity =0.5;//NormalPrivateBrush _ usercolor;Private Double_ Useropacity;Public StringWatermarktext {Get;Set;}
3. Rewrite the ongotfocus () and onlostfocus () events.
In Textbox, we can find that these two events are marked by override, so we can reload them.
Protected Override Void Ongotfocus (routedeventargs e ){ If ( This . Text = Watermarktext ){ This . Text = "" ; This . Foreground = _ Usercolor; This . Opacity = _ Useropacity ;} Base . Ongotfocus (E );} Protected Override Void Onlostfocus (routedeventargs e ){ If ( This . Text. Length < 1 ){ This . Text = Watermarktext; This . Foreground = _ Redcolor; This . Opacity = _ Halfopacity ;} Base . Onlostfocus (E );}
4. Although most of the work has been completed here, there is another important point.
Similar to initialization, the system checks whether a watermark exists and sets a watermark. I willCodeWritten in the sizechanged event. For more information about the control lifecycle, see the Silverlight control lifecycle-Neil Chen. In addition, we need to initialize _ usercolor and _ useropacity.
The code for the sizechanged event is as follows:
Public Mytextbox () {sizechanged + =New Sizechangedeventhandler (mytextbox_sizechanged );} Void Mytextbox_sizechanged ( Object Sender, sizechangedeventargs e) {_ usercolor = This . Foreground; _ useropacity = This . Opacity; If (Watermarktext! = "" ){ This . Foreground =_ Redcolor; This . Opacity = _ Halfopacity; This . Text = Watermarktext ;}}
5. Source code , So far the work has been completed. The complete code is as follows: Textbox with watermark
Using System; Using System. net; Using System. windows; Using System. Windows. controls; Using System. Windows. documents; Using System. Windows. Ink; Using System. Windows. input; Using System. Windows. Media; Using System. Windows. Media. animation; Using System. Windows. shapes; Namespace Textboxwatermark { Public Class Mytextbox: textbox { // Watermark status Private Brush _ redcolor = New Solidcolorbrush (colors. Red ); Private Double _ Halfopacity = 0.5 ; // Normal Private Brush _ usercolor; Private Double _ Useropacity; Public String Watermarktext { Get ; Set ;} Public Mytextbox () {sizechanged + = New Sizechangedeventhandler (mytextbox_sizechanged );} Void Mytextbox_sizechanged ( Object Sender, sizechangedeventargs e) {_ usercolor = This . Foreground; _ useropacity = This . Opacity; If (Watermarktext! = "" ){ This . Foreground = _ Redcolor; This . Opacity = _ Halfopacity; This . Text = Watermarktext ;}} Protected Override Void Ongotfocus (routedeventargs e ){ If ( This . Text = Watermarktext ){ This . Text = "" ; This . Foreground = _ Usercolor; This . Opacity = _ Useropacity ;} Base . Ongotfocus (E );} Protected Override Void Onlostfocus (routedeventargs e ){ If ( This . Text. Length <1 ){ This . Text = Watermarktext; This . Foreground = _ Redcolor; This . Opacity = _ Halfopacity ;} Base . Onlostfocus (e );}}}
6. Call Process
<Local: mytextboxForeground= "Blue"Watermarktext= "Please input! " />
Local is the namespace where the mytextbox class is located. The local code is written as follows: xmlns: Local = "CLR-namespace: textboxwatermark"
As follows:
Focus not obtained:
Get focus and enter
Poor memory