Los textbox con watermark o “marca de agua” son un control muy común en casi cualquier sitio, y por alguna razón Silverlight 4 no viene ya con un control de este tipo, aunque el textbox si tiene una propiedad que se llama Watermark, no se puede usar…

Así que aquí les dejo un textbox con watermark que realizé para un proyecto (el cual pueden descargar al final del artículo o probarlo aquí), fué también mi primer Custom Control en Silverlight, así que cualquier comentario o correción son muy bienvenidos.

El código fuente es el siguiente, cómo se puede ver, el control cuenta con sólo dos propiedades, una para modificar el texto del watermark y otra para cambiar la fuente del watermark:


/// <summary>
    /// A TextBox control with a watermark
    /// </summary>

    [TemplatePart(Name = WatermarkedTextbox.watermarkTextBlock, Type = typeof(TextBlock))]
    public class WatermarkedTextbox : TextBox
    {

        #region Constants
        private const string watermarkTextBlock = "PART_Watermark";
        #endregion

        #region Properties

        public static readonly DependencyProperty Watermark_Property = DependencyProperty.Register("Watermark_", typeof(string), typeof(WatermarkedTextbox),
            new PropertyMetadata("watermark"));

        public static readonly DependencyProperty WatermarkFontFamilyProperty = DependencyProperty.Register(
            "WatermarkFontStyle", typeof(FontFamily), typeof(WatermarkedTextbox),
            new PropertyMetadata(new FontFamily("Segoe Print")));

        /// <summary>
        /// Gets or sets the text for the watermark
        /// </summary>
        [Category("Watermark")]
        [Description("Gets or sets the text for the watermark")]
        public string Watermark_
        {
            get { return (string)GetValue(Watermark_Property); }
            set { SetValue(Watermark_Property, value); }
        }

        /// <summary>
        /// Gets or sets the fontfamily for the watermark
        /// </summary>
        [Category("Watermark")]
        [Description("Gets or sets the text font for the watermark")]
        public FontFamily WatermarkFontFamily
        {
            get { return (FontFamily)GetValue(WatermarkFontFamilyProperty); }
            set { SetValue(WatermarkFontFamilyProperty, value); }
        }

        #endregion

        private Boolean watermarked;

        public WatermarkedTextbox()
        {
            DefaultStyleKey = typeof(WatermarkedTextbox);
            watermarked = true;
            this.GotFocus += new RoutedEventHandler(WatermarkedTextbox_GotFocus);
            this.LostFocus += new RoutedEventHandler(WatermarkedTextbox_LostFocus);
            this.IsEnabledChanged += new DependencyPropertyChangedEventHandler(WatermarkedTextbox_IsEnabledChanged);
            this.TextChanged += new TextChangedEventHandler(WatermarkedTextbox_TextChanged);
        }

        void WatermarkedTextbox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (watermarked)
                Unwatermark();
            if (this.Text == string.Empty)
                Watermark();
        }

        void WatermarkedTextbox_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (IsEnabled)
                GotoVisualState(WatermarkedTextboxVisualStates.Normal, true);
            else
                GotoVisualState(WatermarkedTextboxVisualStates.Disabled, true);
        }

        void WatermarkedTextbox_LostFocus(object sender, RoutedEventArgs e)
        {
            if (this.Text == string.Empty)
                Watermark();
        }

        void WatermarkedTextbox_GotFocus(object sender, RoutedEventArgs e)
        {
            if (this.Text == string.Empty)
                Unwatermark();
            else
                this.SelectAll();
        }

        private void GotoVisualState(WatermarkedTextboxVisualStates VisualState, bool useTransitions)
        {
            VisualStateManager.GoToState(this, VisualState.ToString(), useTransitions);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            if (this.Text != string.Empty)
                Unwatermark();
        }

        private void Watermark()
        {
            GotoVisualState(WatermarkedTextboxVisualStates.Watermarked, false);
            watermarked = true;
        }

        private void Unwatermark()
        {
            GotoVisualState(WatermarkedTextboxVisualStates.Unwatermarked, false);
            watermarked = false;
        }
    }
    public enum WatermarkedTextboxVisualStates { Normal, Disabled, Watermarked, Unwatermarked }

 

Descarga el control completo aquí o pruébalo aquí