本文最后更新于206 天前,其中的信息可能已经过时,如有错误请发送邮件到2289035571@QQ.COM
感谢 TurboAI对本博客的的大力赞助。 创作不易,如果您觉得有帮助,请支持LIncol29!
PasswordBox的属性绑定
1.新建PasswordBoxHelper 使用propa创建Pwd以及IsBindPwd两个附加属性。
- Pwd附加属性中,默认值为string.Empty 当值发生改变时触发回调函数OnPwdChanged
- 在OnPwdChanged函数中将改变的值传给PasswordBox的Password
- IsBindPwd附加属性中,new PropertyMetadata(false, OnPropertyChanged),默认值为false 当为true时,触发OnPropertyChanged
- OnPropertyChanged当为新值,给PasswordBox的PasswordChanged事件绑定方法Pbox_PasswordChanged
- Pbox_PasswordChanged方法主要是调用SetPwd方法,设置Pwd附加属性
详细代码如下
public class PasswordBoxHelper
{
public static string GetPwd(DependencyObject obj)
{
return (string)obj.GetValue(PwdProperty);
}
public static void SetPwd(DependencyObject obj, string value)
{
obj.SetValue(PwdProperty, value);
}
//new PropertyMetadata(string.Empty, OnPwdChanged)
//默认值为string.Empty 当值发生改变时触发回调函数OnPwdChanged
// Using a DependencyProperty as the backing store for Pwd. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PwdProperty =
DependencyProperty.RegisterAttached("Pwd", typeof(string), typeof(PasswordBoxHelper), new PropertyMetadata(string.Empty, OnPwdChanged));
private static void OnPwdChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
PasswordBox pbox = d as PasswordBox;
if (pbox == null)
return;
pbox.Password = (string)e.NewValue;
SetSelection(pbox, pbox.Password.Length, 0);
}
/// <summary>
/// 设置光标位置
/// </summary>
/// <param name="passwordBox"></param>
/// <param name="start">光标开始位置</param>
/// <param name="length">选中长度</param>
private static void SetSelection(PasswordBox passwordBox, int start, int length)
{
passwordBox.GetType()
.GetMethod("Select", BindingFlags.Instance | BindingFlags.NonPublic)
.Invoke(passwordBox, new object[] { start, length
});
}
public static bool GetIsBindPwd(DependencyObject obj)
{
return (bool)obj.GetValue(IsBindPwdProperty);
}
public static void SetIsBindPwd(DependencyObject obj, bool value)
{
obj.SetValue(IsBindPwdProperty, value);
}
// Using a DependencyProperty as the backing store for IsBindPwd. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsBindPwdProperty =
DependencyProperty.RegisterAttached("IsBindPwd", typeof(bool), typeof(PasswordBoxHelper), new PropertyMetadata(false, OnPropertyChanged));
private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
PasswordBox pbox = d as PasswordBox;
if (pbox == null)
return;
if ((bool)e.NewValue)
pbox.PasswordChanged += Pbox_PasswordChanged;
if ((bool)e.OldValue)
pbox.PasswordChanged -= Pbox_PasswordChanged;
}
private static void Pbox_PasswordChanged(object sender, RoutedEventArgs e)
{
PasswordBox pwd = (PasswordBox)sender;
//设置附加属性的值
SetPwd(pwd, pwd.Password);
}
2.新建一个MyPasswordVm 依赖属性,用来绑定PasswordBox
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
public string MyPasswordVM
{
get { return (string)GetValue(MyPasswordVMProperty); }
set { SetValue(MyPasswordVMProperty, value); }
}
// Using a DependencyProperty as the backing store for MyPasswordVM. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyPasswordVMProperty =
DependencyProperty.Register("MyPasswordVM", typeof(string), typeof(MainWindow));
private void Button_Click(object sender, RoutedEventArgs e)
{
MyPasswordVM = "258";
//MessageBox.Show(MyPasswordVM);
}
3.前端界面展示
-
PasswordBox绑定一下两下附加属性
-
local:PasswordBoxHelper.IsBindPwd=“True”
-
local:PasswordBoxHelper.Pwd=”{Binding MyPasswordVM,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}”