#region 登录信息/// <summary> /// 密码 /// </summary> private string _Pwd; /// <summary> /// 密码 /// </summary> public string Pwd { get { return _Pwd; } set { _Pwd = value; RaisePropertyChanged(); } } #endregion
/// <summary> /// PasswordBox扩展属性 /// </summary> public class PasswordBoxExtend { public static string GetPwd(DependencyObject obj) { return (string)obj.GetValue(PwdProperty); } public static void SetPwd(DependencyObject obj, string value) { obj.SetValue(PwdProperty, value); } // 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(PasswordBoxExtend), new PropertyMetadata("", OnPwdChanged)); /// <summary> /// 自定附加属性发生变化 改变Password属性 /// </summary> /// <param name="d"></param> /// <param name="e"></param> private static void OnPwdChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { PasswordBox pwdBox = d as PasswordBox; string newPwd = (string)e.NewValue;//新的值 if (pwdBox != null && pwdBox.Password != newPwd) { pwdBox.Password = newPwd; } } } /// <summary> /// Password行为 Password变化,自定义附加属性跟着变化 /// </summary> public class PasswordBoxBehavior : Behavior<PasswordBox> { /// <summary> /// 附加 注入事件 /// </summary> protected override void OnAttached() { base.OnAttached(); AssociatedObject.PasswordChanged += OnPasswordChanged; } /// <summary> /// Password变化,自定义附加属性跟着变化 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnPasswordChanged(object sender, RoutedEventArgs e) { PasswordBox passwordBox = sender as PasswordBox; string password = PasswordBoxExtend.GetPwd(passwordBox);//附加属性值 if (passwordBox != null && passwordBox.Password != password) { PasswordBoxExtend.SetPwd(passwordBox, passwordBox.Password); } } /// <summary> /// 销毁 移出事件 /// </summary> protected override void OnDetaching() { base.OnDetaching(); AssociatedObject.PasswordChanged -= OnPasswordChanged; } }
xmlns:pwdEx="clr-namespace:LogInModule.Extensions"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
<StackPanel Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Right"> <TextBlock Text="密码:" /> </StackPanel> <StackPanel Grid.Row="2" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center"> <PasswordBox Style="{StaticResource pswbox}" /> </StackPanel>
<Style x:Key="pswbox" TargetType="PasswordBox"> <Setter Property="Height" Value="30"/> <Setter Property="Width" Value="300"/> <Setter Property="Background" Value="#7FFF00"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="PasswordBox"> <Border Background="{TemplateBinding Background}" CornerRadius="15" BorderThickness="1" BorderBrush="#C0FF3E" > <StackPanel> <Grid> <PasswordBox Margin="0,7" Background="#7FFF00" BorderBrush="Transparent" pwdEx:PasswordBoxExtend.Pwd="{Binding Pwd,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" BorderThickness="0" > <i:Interaction.Behaviors> <pwdEx:PasswordBoxBehavior></pwdEx:PasswordBoxBehavior> </i:Interaction.Behaviors> </PasswordBox> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </StackPanel> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style>
标签:01,string,PasswordBox,Binding,Pwd,passwordBox,Password,public From: https://www.cnblogs.com/MingQiu/p/18199231