((MyObject)DataContext).SecurePassword = MyPassword. this should trigger binding and therefore validation PasswordBox Class (Windows.UI.Xaml.Controls) - Windows UWP applications Microsoft Learn Windows. 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) Let's suppose I have an MVVM object like this, with WPF validation using IDataErrorInfo: public class MyObject : INotifyPropert圜hanged, IDataErrorInfo Any idea?Īnother solution, without using any "unsecure" string in the middle, is to adapt the Window code, something like this: But for my PasswordBox never gets the value. If we made any mistake (defined on my Validation class), when I do this: if (!beUserName.HasError & !bePassword.HasError)Įach BindingExpression should say true of false depending on error validations. If (bePassword != null) bePassword.UpdateSource() If (beUserName != null) beUserName.UpdateSource() Īnd this is how I get it for the PasswordBox: BindingExpression bePassword = textBoxUserPass.GetBindingExpression(PasswordBoxAssistant.BoundPassword) 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. While it would make sense to provide the SecurePassword directly from the binding, it always seems to provide an empty value.I'm trying to make a validation for a PasswordBox. Var credential = new (UserName, password) LoginCancel = new DelegateCommand(o => OnLoginCancel(), (o) => IsLoginVisible) LoginAccept = new DelegateCommand(o => OnLogin(o), (o) => IsLoginVisible) Public static readonly DependencyProperty PasswordProperty = DependencyProperty.Register("Password", typeof(string), typeof(BindablePasswordBox), new PropertyMetadata(string.Empty, OnDependencyPropert圜hanged)) public class BindablePasswordBox : Decorator It registers a Propert圜hangedCallback and updates the PasswordBox's Password when it is called. Below is a modified version of it that works in both directions. It only works in one direction, PasswordBox to PasswordProperty. There is an issue with the BindablePasswordBox.
0 Comments
Leave a Reply. |