如何使用MVVM模式或caliburn micro清除PasswordBox值

问题描述 投票:0回答:1

输入错误时如何清除密码框值?我看到过一些答案,例如“将密码框发送到视图模型”,但这只是破坏了 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);
            }
        }
wpf mvvm data-binding caliburn.micro
1个回答
0
投票

我发现你可以通过切换绑定的 IsEnabled 值并使用来欺骗它

    private void PasswordBox_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        if (sender is PasswordBox passwordBox 
            && e.NewValue is bool newValue
            && !newValue)
        {
            passwordBox.Clear();
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.