Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

PasswordBox Helper

public static class PasswordBoxHelper
{
    public static readonly DependencyProperty BoundPasswordProperty =
        DependencyProperty.RegisterAttached("BoundPassword",
            typeof(string),
            typeof(PasswordBoxHelper),
            new FrameworkPropertyMetadata(string.Empty, OnBoundPasswordChanged));

    public static string GetBoundPassword(DependencyObject d)
    {
        var box = d as PasswordBox;
        if (box != null)
        {
            // this funny little dance here ensures that we've hooked the
            // PasswordChanged event once, and only once.
            box.PasswordChanged -= PasswordChanged;
            box.PasswordChanged += PasswordChanged;
        }

        return (string)d.GetValue(BoundPasswordProperty);
    }

    public static void SetBoundPassword(DependencyObject d, string value)
    {
        if (string.Equals(value, GetBoundPassword(d)))
            return; // and this is how we prevent infinite recursion

        d.SetValue(BoundPasswordProperty, value);
    }

    private static void OnBoundPasswordChanged(
        DependencyObject d,
        DependencyPropertyChangedEventArgs e)
    {
        var box = d as PasswordBox;

        if (box == null)
            return;

        box.Password = GetBoundPassword(d);
    }

    private static void PasswordChanged(object sender, RoutedEventArgs e)
    {
        PasswordBox password = sender as PasswordBox;

        SetBoundPassword(password, password.Password);

        // set cursor past the last character in the password box
        password.GetType().GetMethod("Select", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(password, new object[] { password.Password.Length, 0 }); 
    }

}
Comment

PREVIOUS NEXT
Code Example
Csharp :: image into sql database 
Csharp :: c# remove numericUpDown white space 
Csharp :: Difference between IHostingEnvironment and IWebHostEnvironment ? 
Csharp :: make all variables nonserizlized unity 
Csharp :: wpf clock conrt 
Csharp :: mydata api .net 
Csharp :: streamwriter delete all text 
Csharp :: WPF combobox filter as you type 
Csharp :: system.collections.generic.list 1 system.int32 c# 
Csharp :: c# variable 
Csharp :: c# .net core kendo dropdownlistfor enum 
Csharp :: add css class based on model value razor 
Csharp :: large blank file C# 
Csharp :: visual studio pre build event not working 
Csharp :: list of countries in .net mvc 5 
Csharp :: c# formula from string 
Csharp :: read barcode with barcode scanner c# winform serial port number 
Csharp :: asp.net core relative file path within console app 
Csharp :: c# wpf datagrid extra column 
Csharp :: reference variable from another script "winforms" c# 
Csharp :: auto scroll infinite scroller unity 
Csharp :: make character move upward forever unity 2d 
Csharp :: linq query languages 
Csharp :: hahhaa i hack u 
Csharp :: Garbage collect every 30 frames unity 
Csharp :: delete an object c# 
Csharp :: Set property of control on form by name 
Csharp :: how to destroy bridges animal crossing 
Csharp :: what is difference between int.Parse and toint32 in c# 
Csharp :: Visual Studio - Summary Tag Comments - Optional Params 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =