Search code examples
textboxwindows-runtimewatermarkmaskedtextboxpasswordbox

How can I add WaterMark property to PasswordBox in Winrt?


I need WatermarkPasswordBox control but there is not in Winrt. Maybe we can add Watermark property to PasswordBox. Are there anyone who can do it?

Thanks


Solution

  • UPDATE 1

    If you don't want to use 3rd party DLL, add these two methods in PasswordBoxBehavior.cs file.

    using System.Reflection;
    
    public static T FindVisualChildByName<T>(this DependencyObject fe, string name) where T : DependencyObject
    {
        if (string.IsNullOrEmpty(name))
        {
            throw new ArgumentNullException("name");
        }
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(fe); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(fe, i);
            string a = child.GetValue(FrameworkElement.NameProperty) as string;
            if (a == name)
            {
                return child as T;
            }
            T t = FindVisualChildByName<T>(child, name);
            if (t != null)
            {
                return t;
            }
        }
        return default(T);
    }
    
    public static T FindVisualParent<T>(this DependencyObject fe) where T : DependencyObject
    {
        for (fe = VisualTreeHelper.GetParent(fe); fe != null; fe = VisualTreeHelper.GetParent(fe))
        {
            T t = fe as T;
            if (t != null)
            {
                return t;
            }
        }
        return default(T);
    }
    

    Here's extensive blog from JulMar

    Adding a watermark to a PasswordBox in a Windows Store app

    Here’s the code if you’d like to use it yourself.