82 lines
3.4 KiB
C#
82 lines
3.4 KiB
C#
|
|
using System.Windows;
|
|||
|
|
using System.Windows.Controls;
|
|||
|
|
using System.Windows.Controls.Primitives;
|
|||
|
|
using System.Windows.Media;
|
|||
|
|
|
|||
|
|
namespace WPFDark.StandardControls
|
|||
|
|
{
|
|||
|
|
public class PasswordBoxHelper
|
|||
|
|
{
|
|||
|
|
public static readonly DependencyProperty IsPasswordVisibleProperty =
|
|||
|
|
DependencyProperty.RegisterAttached(
|
|||
|
|
"IsPasswordVisible",
|
|||
|
|
typeof(bool),
|
|||
|
|
typeof(PasswordBoxHelper),
|
|||
|
|
new PropertyMetadata(false, OnIsPasswordVisibleChanged));
|
|||
|
|
|
|||
|
|
public static bool GetIsPasswordVisible(DependencyObject obj)
|
|||
|
|
{ return (bool)obj.GetValue(IsPasswordVisibleProperty); }
|
|||
|
|
|
|||
|
|
public static void SetIsPasswordVisible(DependencyObject obj, bool value)
|
|||
|
|
{ obj.SetValue(IsPasswordVisibleProperty, value); }
|
|||
|
|
private static object? OriginContent { get; set; }
|
|||
|
|
//TODO
|
|||
|
|
private static void OnIsPasswordVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
|||
|
|
{
|
|||
|
|
if(d is PasswordBox passwordBox)
|
|||
|
|
{
|
|||
|
|
bool isVisible = (bool)e.NewValue;
|
|||
|
|
|
|||
|
|
if(isVisible)
|
|||
|
|
{
|
|||
|
|
// 创建一个TextBox来显示密码
|
|||
|
|
var textBox = new TextBox
|
|||
|
|
{
|
|||
|
|
Text = passwordBox.Password,
|
|||
|
|
FontSize= passwordBox.FontSize,
|
|||
|
|
FontFamily = passwordBox.FontFamily,
|
|||
|
|
FontWeight = passwordBox.FontWeight,
|
|||
|
|
FontStyle = passwordBox.FontStyle,
|
|||
|
|
HorizontalContentAlignment = passwordBox.HorizontalContentAlignment,
|
|||
|
|
VerticalContentAlignment = passwordBox.VerticalContentAlignment,
|
|||
|
|
Padding = passwordBox.Padding,
|
|||
|
|
Margin = passwordBox.Margin,
|
|||
|
|
BorderThickness = passwordBox.BorderThickness,
|
|||
|
|
BorderBrush = passwordBox.BorderBrush,
|
|||
|
|
Background = passwordBox.Background,
|
|||
|
|
Foreground = passwordBox.Foreground
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// 将TextBox的Text和PasswordBox的Password进行双向绑定
|
|||
|
|
textBox.TextChanged += (s, args) =>
|
|||
|
|
{
|
|||
|
|
if(s is TextBox tb)
|
|||
|
|
{
|
|||
|
|
passwordBox.Password = tb.Text;
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// 替换ContentHost的内容
|
|||
|
|
if(passwordBox.Template.FindName("PART_ContentHost", passwordBox) is ScrollViewer scrollViewer)
|
|||
|
|
{
|
|||
|
|
OriginContent = scrollViewer.Content; // 保存原始内容
|
|||
|
|
scrollViewer.Content = textBox;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
// 还原到PasswordBox的默认状态
|
|||
|
|
if(passwordBox.Template.FindName("PART_ContentHost", passwordBox) is ScrollViewer scrollViewer)
|
|||
|
|
{
|
|||
|
|
string currentPassword = passwordBox.Password;
|
|||
|
|
if(currentPassword != null)
|
|||
|
|
{
|
|||
|
|
scrollViewer.Content = OriginContent; // 清除TextBox
|
|||
|
|
passwordBox.Password = currentPassword; // 保持密码不变
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|