Not knowing your CustomPasswordBox
, sfTextInputLayout
and Command
, I used the XAML code and create MyCommand as a test:
<Window.DataContext>
<local:LoginVM></local:LoginVM>
</Window.DataContext>
<StackPanel>
<TextBox Foreground="BlanchedAlmond" Text="{Binding user.Username, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="120" Height="30" />
<TextBox Text="{Binding user.Password, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="120" Height="30" />
<Button
Margin="80,20,80,0"
Height="30"
Content="Sign in"
Command="{Binding ClickToLogCommand}"
>
</Button>
</StackPanel>
The whole c# code is:
public class User: NotifyObject
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[MaxLength(50)]
public string Name { get; set; }
[MaxLength(50)]
public string Lastname { get; set; }
private string _Username;
public string Username
{
get { return _Username; }
set
{
if (_Username != value)
{
_Username = value;
RaisePropertyChanged("Username");
}
}
}
private string _Password;
public string Password
{
get { return _Password; }
set
{
if (_Password != value)
{
_Password = value;
RaisePropertyChanged("Password");
}
}
}
}
public class NotifyObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class LoginVM : NotifyObject
{
private User _user;
public User user
{
get { return _user; }
set
{
if (_user != value)
{
_user = value;
RaisePropertyChanged("user");
}
}
}
private MyCommand _ClickToLogCommand;
public MyCommand ClickToLogCommand
{
get
{
if (_ClickToLogCommand == null)
_ClickToLogCommand = new MyCommand(new Action<object>
(
o =>
{
Debug.WriteLine($" user name = {user.Username} password = {user.Password}");
}
),
new Func<object, bool>(o =>(!string.IsNullOrEmpty(user.Username)&&!string.IsNullOrEmpty(user.Password))));
return _ClickToLogCommand;
}
}
public LoginVM()
{
user = new User();
}
}
public class MyCommand : ICommand
{
public event EventHandler CanExecuteChanged
{
add
{
if (_canExecute != null)
{
CommandManager.RequerySuggested += value;
}
}
remove
{
if (_canExecute != null)
{
CommandManager.RequerySuggested -= value;
}
}
}
private Func<object, bool> _canExecute;
private Action<object> _execute;
public MyCommand(Action<object> execute) : this(execute, null)
{
}
public MyCommand(Action<object> execute, Func<object, bool> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
public MyCommand(Action<object> execute, bool keepTargetAlive = false)
{
_execute = execute;
}
public bool CanExecute(object parameter)
{
if (_canExecute == null) return true;
return _canExecute(parameter);
}
public void Execute(object parameter)
{
if (_execute != null && CanExecute(parameter))
{
_execute(parameter);
}
}
}
The result picture is:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.