schangxiang@126.com
2025-11-04 ca398287db3f5ea01f03aac4a85edb13b28100a6
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
 
namespace XHandler.Controls
{
    /// <summary>
    /// 按照步骤 1a 或 1b 操作,然后执行步骤 2 以在 XAML 文件中使用此自定义控件。
    ///
    /// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
    /// 元素中:
    ///
    ///     xmlns:MyNamespace="clr-namespace:HxUserManagement.Controls"
    ///
    ///
    /// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
    /// 元素中:
    ///
    ///     xmlns:MyNamespace="clr-namespace:HxUserManagement.Controls;assembly=HxUserManagement.Controls"
    ///
    /// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用,
    /// 并重新生成以避免编译错误:
    ///
    ///     在解决方案资源管理器中右击目标项目,然后依次单击
    ///     “添加引用”->“项目”->[浏览查找并选择此项目]
    ///
    ///
    /// 步骤 2)
    /// 继续操作并在 XAML 文件中使用控件。
    ///
    ///     <MyNamespace:HxPasswordBox/>
    ///
    /// </summary>
    public class HxPasswordBox : Control
    {
        public event EventHandler PasswordChanged;
        static HxPasswordBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(HxPasswordBox), new FrameworkPropertyMetadata(typeof(HxPasswordBox)));
        }
 
        /// <summary>
        /// 密码
        /// </summary>
        public string Password
        {
            get => (string)GetValue(PasswordProperty);
            set => SetValue(PasswordProperty, value);
        }
 
        public static readonly DependencyProperty PasswordProperty =
           DependencyProperty.Register("Password", typeof(string), typeof(HxPasswordBox), new PropertyMetadata(null, OnPasswordChanged));
 
        public static void OnPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            HxPasswordBox control = (HxPasswordBox)d;
            if (control.GetTemplateChild("PART_PasswrodText") is TextBox txt && control.GetTemplateChild("PART_Password") is PasswordBox pb)
            {
                txt.Text = (string)control.GetValue(PasswordProperty);
                pb.Password = (string)control.GetValue(PasswordProperty);
            }
        }
 
        /// <summary>
        /// 水印
        /// </summary>
        public string WaterMark
        {
            get => (string)GetValue(WaterMarkProperty);
            set => SetValue(WaterMarkProperty, value);
        }
 
        public static readonly DependencyProperty WaterMarkProperty =
           DependencyProperty.Register("WaterMark", typeof(string), typeof(HxPasswordBox), new PropertyMetadata(null));
 
 
        /// <summary>
        /// 显示水印
        /// </summary>
        public bool ShowWaterMark
        {
            get => (bool)GetValue(ShowWaterMarkProperty);
            set => SetValue(ShowWaterMarkProperty, value);
        }
 
        public static readonly DependencyProperty ShowWaterMarkProperty =
           DependencyProperty.Register("ShowWaterMark", typeof(bool), typeof(HxPasswordBox), new PropertyMetadata(false));
 
        /// <summary>
        /// 存在文本内容
        /// </summary>
        public bool HasText
        {
            get => (bool)GetValue(HasTextProperty);
            set => SetValue(HasTextProperty, value);
        }
 
        public static readonly DependencyProperty HasTextProperty =
           DependencyProperty.Register("HasText", typeof(bool), typeof(HxPasswordBox), new PropertyMetadata(false));
 
        /// <summary>
        /// 密码字符
        /// </summary>
        public char PasswrodChar
        {
            get => (char)GetValue(PasswrodCharProperty);
            set => SetValue(PasswrodCharProperty, value);
        }
 
        public static readonly DependencyProperty PasswrodCharProperty =
           DependencyProperty.Register("PasswrodChar", typeof(char), typeof(HxPasswordBox), new PropertyMetadata('*'));
 
        private TextBox PART_PasswrodText { get; set; }
        private PasswordBox PART_Password { get; set; }
 
 
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            PART_PasswrodText = GetTemplateChild("PART_PasswordText") as TextBox;
            PART_Password = GetTemplateChild("PART_Password") as PasswordBox;
            if(PART_PasswrodText!=null && PART_Password!=null)
            {
                PART_PasswrodText.TextChanged += PART_PasswrodText_TextChanged;
                PART_Password.PasswordChanged += PART_Password_PasswordChanged;
                PART_Password.Password = Password;
                HasText = !string.IsNullOrEmpty(Password);
            }
        }
 
        private void PART_Password_PasswordChanged(object sender, RoutedEventArgs e)
        {
            Password = PART_Password.Password;
            PART_PasswrodText.Text = Password;
            PART_Password.GetType().GetMethod("Select", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(PART_Password, new object[] { PART_Password.Password.Length, 0 });
            PasswordChanged?.Invoke(this, EventArgs.Empty);
 
        }
 
        private void PART_PasswrodText_TextChanged(object sender, TextChangedEventArgs e)
        {
            Password= PART_PasswrodText.Text;
            PART_Password.Password = Password;
            HasText= !string.IsNullOrEmpty(Password);
            PasswordChanged?.Invoke(this, EventArgs.Empty);
        }
    }
}