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 HxUserManagement.Controls
{
///
/// 按照步骤 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 文件中使用控件。
///
///
///
///
public class HxPasswordBox : Control
{
public event EventHandler PasswordChanged;
static HxPasswordBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(HxPasswordBox), new FrameworkPropertyMetadata(typeof(HxPasswordBox)));
}
///
/// 密码
///
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);
}
}
///
/// 水印
///
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));
///
/// 显示水印
///
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));
///
/// 存在文本内容
///
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));
///
/// 密码字符
///
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);
}
}
}