14

我正在尝试对PasswordBox. 为了进行验证,我遵循了这个链接,它显示了如何验证TextBox

问题随之而来PasswordBoxes。因为Password出于安全原因它不可绑定,所以我尝试在此链接之后进行绑定(也在这里解释,对于 CodeProject 用户)。

所以,显然,太棒了!我可以绑定我PasswordBoxPassword属性,然后我可以绑定我的验证。但它无视我...

这是TextBox我使用并且工作正常的常规:

<local:ErrorProvider Grid.Column="1" Grid.Row="2" >
    <TextBox Width="160" 
          HorizontalAlignment="Left" 
           Name="textBoxUserPass" 
           Text="{Binding Path=Password, UpdateSourceTrigger=Explicit}" />
 </local:ErrorProvider>

这是PasswordBox我试图模拟的:

<local:ErrorProvider Grid.Column="1" Grid.Row="2" >
      <PasswordBox Width="160"
          HorizontalAlignment="Left"
          Name="textBoxUserPass"
          local:PasswordBoxAssistant.BindPassword="True"
          local:PasswordBoxAssistant.BoundPassword="{Binding Path=Password, UpdateSourceTrigger=Explicit}" />
 </local:ErrorProvider>

这就是我获得BindingExpressionfor each的方式TextBox

BindingExpression beUserName = textBoxUserName.GetBindingExpression(TextBox.TextProperty);
if (beUserName != null) beUserName.UpdateSource();

这就是我得到它的方式PasswordBox

BindingExpression bePassword = textBoxUserPass.GetBindingExpression(PasswordBoxAssistant.BoundPassword);
if (bePassword != null) bePassword.UpdateSource();

如果我们犯了任何错误(在我的 Validation 类中定义),当我这样做时:

if (!beUserName.HasError && !bePassword.HasError)

根据错误验证,每个人都BindingExpression应该说假。但是因为我PasswordBox从来没有得到过价值......有什么想法吗?

4

4 回答 4

12

尝试设置ValidatesOnDataErrors=TrueValidatesOnExceptions=True绑定:

<PasswordBox ...
   local:PasswordBoxAssistant.BoundPassword="{Binding Path=Password,
      UpdateSourceTrigger=Explicit, 
      ValidatesOnDataErrors=True, 
      ValidatesOnExceptions=True}"
/>
于 2013-02-27T14:50:17.303 回答
3

在绑定上设置 Mode=TwoWay

local:PasswordBoxAssistant.BoundPassword="{Binding Path=Password,Mode=TwoWay,
UpdateSourceTrigger=Explicit}"
于 2013-07-03T19:36:26.143 回答
2

据我记得,在 PasswordBox 上添加验证的唯一方法是在 SecurePassword 的绑定属性的设置器中抛出一个新的 ValidationException。PasswordBoxAssistant 不会帮助您。

于 2013-02-27T13:49:10.637 回答
2

另一种解决方案,在中间不使用任何“不安全”字符串,是调整 Window 代码,如下所示:

假设我有一个这样的 MVVM 对象,使用 WPF 验证IDataErrorInfo

public class MyObject : INotifyPropertyChanged, IDataErrorInfo
{
    ...
    public SecureString SecurePassword
    {
        get
        { ... }
        set
        {
            ...
            OnPropertyChanged("SecurePassword");
        }
    }

    ...

    string IDataErrorInfo.Error { get { return Validate(null); } }
    string IDataErrorInfo.this[string columnName] { get { return Validate(columnName); } }

    private string Validate(string memberName)
    {
        string error = null;
        ...
        if (memberName == "SecurePassword" || memberName == null)
        {
            // this is where I code my custom business rule
            if (SecurePassword == null || SecurePassword.Length == 0)
            {
                error = "Password must be specified.";
            }
        }
        ...
        return error;
    }

}

还有一个带有 PasswordBox 的 Window Xaml,如下所示:

<PasswordBox Name="MyPassword" PasswordChanged="MyPassword_Changed" ... />

然后,像这样对应的 Window 代码会触发 PasswordBox 绑定:

// add a custom DependencyProperty
public static readonly DependencyProperty SecurePasswordProperty =
    DependencyProperty.RegisterAttached("SecurePassword", typeof(SecureString), typeof(MyWindow));

public MyWindow()
{
    InitializeComponent();

    DataContext = myObject; // created somewhere

    // create a binding by code
    Binding passwordBinding = new Binding(SecurePasswordProperty.Name);
    passwordBinding.Source = myObject;
    passwordBinding.ValidatesOnDataErrors = true;
    // you can configure other binding stuff here
    MyPassword.SetBinding(SecurePasswordProperty, passwordBinding);
}

private void MyPassword_Changed(object sender, RoutedEventArgs e)
{
    // this should trigger binding and therefore validation
    ((MyObject)DataContext).SecurePassword = MyPassword.SecurePassword;
}
于 2014-05-15T17:01:31.723 回答