This is how I get the BindingExpression for each TextBox: BindingExpression beUserName = textBoxUserName.GetBindingExpression(TextBox.TextProperty) This is a regular TextBox that I use and works fine: Īnd this is the PasswordBox I tried to simulate: So, apparently, fantastic! I can bind my PasswordBox with its Password property, so then I can bind with my validation. Because its Password is not bindable due to security reasons, I tried to make a binding following this link (also explained here, for CodeProject users). For making validations I followed this link, that shows how to validate on TextBox. I'm trying to make a validation for a PasswordBox. ((MyObject)DataContext).SecurePassword = MyPassword.SecurePassword this should trigger binding and therefore validation Private void MyPassword_Changed(object sender, RoutedEventArgs e) MyPassword.SetBinding(SecurePasswordProperty, passwordBinding) you can configure other binding stuff here PasswordBinding.ValidatesOnDataErrors = true Public static readonly DependencyProperty SecurePasswordProperty =ĭependencyProperty.RegisterAttached("SecurePassword", typeof(SecureString), typeof(MyWindow)) ĭataContext = myObject // created somewhereīinding passwordBinding = new Binding(SecurePasswordProperty.Name) Then, the corresponding Window code like this will trigger PasswordBox binding: // add a custom DependencyProperty If (SecurePassword = null || SecurePassword.Length = 0)Īnd a Window Xaml with a PasswordBox like this: this is where I code my custom business rule If (memberName = "SecurePassword" || memberName = null) Private string Validate(string memberName) Set Mode=TwoWay on your binding local:PasswordBoxAssistant.BoundPassword=" Please don't forget to up vote answers you like or which help you and mark one(s) which answer your question.Try setting ValidatesOnDataErrors=True and ValidatesOnExceptions=True on your binding: SetPassword(passwordBox, passwordBox.Password) Private static void PasswordChanged(object sender, RoutedEventArgs e) Private static void Attach(DependencyObject sender, PasswordBox.PasswordChanged += PasswordChanged PasswordBox.Password = (string)e.NewValue PasswordBox.PasswordChanged -= PasswordChanged PasswordBox passwordBox = sender as PasswordBox Private static void OnPasswordPropert圜hanged(DependencyObject sender, Private static void SetIsUpdating(DependencyObject dp, bool value) Return (bool)dp.GetValue(IsUpdatingProperty) Private static bool GetIsUpdating(DependencyObject dp) Public static void SetPassword(DependencyObject dp, string value) Return (string)dp.GetValue(PasswordProperty) Public static string GetPassword(DependencyObject dp) Return (bool)dp.GetValue(AttachProperty) Public static bool GetAttach(DependencyObject dp) Public static void SetAttach(DependencyObject dp, bool value) Private static readonly DependencyProperty IsUpdatingProperty =ĭependencyProperty.RegisterAttached("IsUpdating", typeof(bool), Typeof(bool), typeof(PasswordHelper), new PropertyMetadata(false, Attach)) Public static readonly DependencyProperty AttachProperty =ĭependencyProperty.RegisterAttached("Attach", New FrameworkPropertyMetadata(string.Empty, OnPasswordPropert圜hanged)) Public static readonly DependencyProperty PasswordProperty =ĭependencyProperty.RegisterAttached("Password", There's an attached property based helper class here: The equivalent is Password, but I think you will find there's an issue in that it's not a dependency property. There is no XAML only solution however if that's what you are looking for. Pb.Tag = (!string.IsNullOrEmpty(pb.Password)).ToString() private void OnPasswordChanged(object sender, RoutedEventArgs e) You could handle the PasswordChanged event of the PasswordBox and set its Tag property to "True" or "False" depending on whether the Password property contains any characters and then check the value of the Tag property in the trigger. There is a Password property but you won't be able to use it like the Text property since it is not a dependency property.
0 Comments
Leave a Reply. |