Vývoj moderních aplikací na platformě Microsoft .NET

Je vyžadována podpora jazyka JavaScript

Některé stránky na tomto webu vyžadují podporu jazyka JavaScript. Váš webový prohlížeč jazyk JavaScript nepodporuje nebo jazyk JavaScript není povolen.

Chcete-li zjistit, zda webový prohlížeč podporuje jazyk JavaScript nebo jazyk JavaScript chcete povolit, přečtěte si nápovědu k vašemu webovému prohlížeči.


PasswordBoxExtender.cs

Download file

Toto je zdrojový kód souboru PasswordBoxExtender.cs

Adds Watermark support to Silverlight PasswordBox control.

using System;
using System.Windows;
using System.Windows.Controls;

namespace IMP.Windows.Controls
{
    /// <summary>
    /// Adds Watermark support to PasswordBox control
    /// </summary>
    /// <remarks>
    /// <c>Watermark</c> support
    /// </remarks>
    public class PasswordBoxExtender
    {
        #region property getters/setters
        /// <summary>
        /// Watermark attached dependency property
        /// </summary>
        public static readonly DependencyProperty WatermarkProperty = DependencyProperty.RegisterAttached("Watermark", typeof(object), typeof(PasswordBoxExtender), new PropertyMetadata(OnWatermarkPropertyChanged));

        /// <summary>
        /// Watermark Property getter
        /// </summary>
        public static object GetWatermark(DependencyObject source)
        {
            return (object)source.GetValue(PasswordBoxExtender.WatermarkProperty);
        }

        /// <summary>
        /// Watermark Property setter
        /// </summary>
        public static void SetWatermark(DependencyObject target, object value)
        {
            target.SetValue(PasswordBoxExtender.WatermarkProperty, value);
        }

        private static void OnWatermarkPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            var passwordBox = sender as PasswordBox;
            if (passwordBox != null)
            {
                passwordBox.Loaded -= PasswordBox_Loaded;
                passwordBox.PasswordChanged -= PasswordBox_PasswordChanged;
                passwordBox.Loaded += PasswordBox_Loaded;
                passwordBox.PasswordChanged += PasswordBox_PasswordChanged;

                OnWatermarkChanged(passwordBox);
                SetWatermarkState(passwordBox, true);
            }
        }

        private static void OnWatermarkChanged(PasswordBox passwordBox)
        {
            Control watermarkControl = passwordBox.GetValue(PasswordBoxExtender.WatermarkProperty) as Control;
            if (watermarkControl != null)
            {
                watermarkControl.IsTabStop = false;
                watermarkControl.IsHitTestVisible = false;
            }
        }
        #endregion

        #region private member functions
        private static void PasswordBox_Loaded(object sender, RoutedEventArgs e)
        {
            ((PasswordBox)sender).ApplyTemplate();
            SetWatermarkState((PasswordBox)sender, false);
        }

        private static void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
        {
            SetWatermarkState((PasswordBox)sender, true);
        }

        private static void SetWatermarkState(PasswordBox passwordBox, bool useTransitions)
        {
            object watermark = passwordBox.GetValue(PasswordBoxExtender.WatermarkProperty);

            // Update the WatermarkStates group
            if (watermark != null && string.IsNullOrEmpty(passwordBox.Password))
            {
                GoToState(passwordBox, useTransitions, "Watermarked", "Unwatermarked");
            }
            else
            {
                GoToState(passwordBox, useTransitions, "Unwatermarked");
            }
        }

        private static void GoToState(Control control, bool useTransitions, params string[] stateNames)
        {
            if (control == null)
            {
                throw new ArgumentNullException("control");
            }

            if (stateNames != null)
            {
                foreach (string str in stateNames)
                {
                    if (VisualStateManager.GoToState(control, str, useTransitions))
                    {
                        return;
                    }
                }
            }
        }
        #endregion
    }
}