1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| using System; using System.Collections.Generic; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Media;
namespace ZView { public class PasswordPlaceholderManager { #region Fields
private static readonly Dictionary<PasswordBox, VisualBrush> TxtBrushes = new Dictionary<PasswordBox, VisualBrush>();
#endregion Fields
#region Attached DependencyProperty
public static readonly DependencyProperty PlaceholderProperty = DependencyProperty.RegisterAttached( "Placeholder", typeof(string), typeof(PasswordPlaceholderManager), new PropertyMetadata("请在此处输入", OnPlaceholderChanged) );
public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached( "Password", typeof(string), typeof(PasswordPlaceholderManager), new PropertyMetadata(string.Empty, OnPasswordPropertyChanged));
public static readonly DependencyProperty IsUpdatingProperty = DependencyProperty.RegisterAttached( "IsUpdating", typeof(bool), typeof(PasswordPlaceholderManager), new PropertyMetadata(default(bool)));
public static void SetIsUpdating(DependencyObject element, bool value) { element.SetValue(IsUpdatingProperty, value); }
public static bool GetIsUpdating(DependencyObject element) { return (bool)element.GetValue(IsUpdatingProperty); }
private static void OnPasswordPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { PasswordBox passwordBox = d as PasswordBox; if (passwordBox != null) { passwordBox.PasswordChanged -= Txt_PasswordChanged; if (!GetIsUpdating(passwordBox)) { passwordBox.Password = e.NewValue.ToString(); }
passwordBox.PasswordChanged += Txt_PasswordChanged; } }
public static string GetPlaceholder(DependencyObject obj) { return (string)obj.GetValue(PlaceholderProperty); }
public static void SetPlaceholder(DependencyObject obj, string value) { obj.SetValue(PlaceholderProperty, value); }
#endregion Attached DependencyProperty
#region Events Handling
public static void OnPlaceholderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if ((d is PasswordBox pb) && (!TxtBrushes.ContainsKey(pb))) { var placeholderTextBlock = new PasswordBox(); var binding = new Binding { Source = pb, Path = new PropertyPath("(0)", PlaceholderProperty) }; placeholderTextBlock.SetBinding(PasswordProperty, binding);
placeholderTextBlock.Opacity = 0.8; placeholderTextBlock.Padding = new Thickness(2, 0, 0, 0); placeholderTextBlock.BorderThickness = new Thickness(0, 0, 0, 0); placeholderTextBlock.Foreground = Brushes.Gray;
var placeholderVisualBrush = new VisualBrush { AlignmentX = AlignmentX.Left, Stretch = Stretch.None, Visual = placeholderTextBlock };
pb.Background = placeholderVisualBrush; pb.PasswordChanged += Txt_PasswordChanged;
pb.Unloaded += PlaceholderPasswordBox_Unloaded;
TxtBrushes.Add(pb, placeholderVisualBrush); } }
public static void SetPassword(DependencyObject element, string value) { element.SetValue(PasswordProperty, value); }
private static void Txt_PasswordChanged(object sender, RoutedEventArgs e) { Console.WriteLine(sender is PasswordBox); if ((sender is PasswordBox pb) && (TxtBrushes.ContainsKey(pb))) { SetIsUpdating(pb, true); SetPassword(pb, pb.Password); SetIsUpdating(pb, false); var placeholderVisualBrush = TxtBrushes[pb]; Console.WriteLine(placeholderVisualBrush); pb.Background = string.IsNullOrEmpty(pb.Password) ? placeholderVisualBrush : null; } }
private static void PlaceholderPasswordBox_Unloaded(object sender, RoutedEventArgs e) { if ((sender is PasswordBox pb) && (TxtBrushes.ContainsKey(pb))) { TxtBrushes.Remove(pb);
pb.PasswordChanged -= Txt_PasswordChanged; pb.Unloaded -= PlaceholderPasswordBox_Unloaded; } }
#endregion Events Handling } }
|