输入错误时如何清除密码框值?我看到过一些答案,例如“将密码框发送到视图模型”,但这只是破坏了 MVVM 模式。
当前 xaml
<PasswordBox x:Name="PasswordInput" Style="{DynamicResource PWbox}" cal:Message.Attach="[Event PasswordChanged] = [Action PasswordChanged($source)]" Grid.Row="3" Grid.Column="2"/>
public void PasswordChanged(PasswordBox source)
{
OldPassword = source.Password;
}
private string _oldPassword;
public string OldPassword
{
get { return _oldPassword; }
set
{
_oldPassword = value;
NotifyOfPropertyChange(() => OldPassword);
}
}
我发现你可以通过切换绑定的 IsEnabled 值并使用来欺骗它
private void PasswordBox_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (sender is PasswordBox passwordBox
&& e.NewValue is bool newValue
&& !newValue)
{
passwordBox.Clear();
}
}