WPF .Net Core 6 RelayCommand Problem
-
I'm trying to create a simple log in window. The Sign In button does not get enabled. The CanExecuteChange fires on startup, but never after that. You can see that I'm using the Community Toolkit. I've also tried the RelayCommand in my own framework. Neither work. I have also tried
CommandManager.InvalidateRequerySuggested();
in the UserName and Password properties. Here's the Sign In button
Here's the View Model
using CommunityToolkit.Mvvm.Input;
using Marois.Framework.Core.Shared;
using System.Windows.Input;namespace Jayhawk.UI.WPF.ViewModels
{
public class LoginViewModel : _DialogViewModelBase
{
#region Private Fields
private readonly IEventAggregator _eventAggregator;
#endregion#region Properties private string? \_UserName; public string? UserName { get { return \_UserName; } set { SetProperty(nameof(UserName), ref \_UserName, value); } } private string? \_Password; public string? Password { get { return \_Password; } set { SetProperty(nameof(Password), ref \_Password, value); } } private bool \_IsRememberMeChecked; public bool IsRememberMeChecked { get { return \_IsRememberMeChecked; } set { SetProperty(nameof(IsRememberMeChecked), ref \_IsRememberMeChecked, value); } } private bool \_IsLoggingIn; public bool IsLoggingIn { get { return \_IsLoggingIn; } set { SetProperty(nameof(IsLoggingIn), ref \_IsLoggingIn, value); } } #endregion #region Commands private ICommand? \_SignInCommand; public ICommand? SignInCommand { get { if (\_SignInCommand == null) \_SignInCommand = new RelayCommand(SignInExecuted, SignInCanExecute); return \_SignInCommand; } } #endregion #region CTOR public LoginViewModel() { } #endregion #region Private Methods priv
-
I'm trying to create a simple log in window. The Sign In button does not get enabled. The CanExecuteChange fires on startup, but never after that. You can see that I'm using the Community Toolkit. I've also tried the RelayCommand in my own framework. Neither work. I have also tried
CommandManager.InvalidateRequerySuggested();
in the UserName and Password properties. Here's the Sign In button
Here's the View Model
using CommunityToolkit.Mvvm.Input;
using Marois.Framework.Core.Shared;
using System.Windows.Input;namespace Jayhawk.UI.WPF.ViewModels
{
public class LoginViewModel : _DialogViewModelBase
{
#region Private Fields
private readonly IEventAggregator _eventAggregator;
#endregion#region Properties private string? \_UserName; public string? UserName { get { return \_UserName; } set { SetProperty(nameof(UserName), ref \_UserName, value); } } private string? \_Password; public string? Password { get { return \_Password; } set { SetProperty(nameof(Password), ref \_Password, value); } } private bool \_IsRememberMeChecked; public bool IsRememberMeChecked { get { return \_IsRememberMeChecked; } set { SetProperty(nameof(IsRememberMeChecked), ref \_IsRememberMeChecked, value); } } private bool \_IsLoggingIn; public bool IsLoggingIn { get { return \_IsLoggingIn; } set { SetProperty(nameof(IsLoggingIn), ref \_IsLoggingIn, value); } } #endregion #region Commands private ICommand? \_SignInCommand; public ICommand? SignInCommand { get { if (\_SignInCommand == null) \_SignInCommand = new RelayCommand(SignInExecuted, SignInCanExecute); return \_SignInCommand; } } #endregion #region CTOR public LoginViewModel() { } #endregion #region Private Methods priv
Looking at the source code for
CommunityToolkit.Mvvm.Input.RelayCommand
[^], it doesn't use theCommandManager
at all. You need to explicitly call the NotifyCanExecuteChanged method[^] instead. NB: ThatSetProperty
method looks odd - any relatively recent framework would use[CallerMemberName]
so that you don't need to pass the name of the property:protected bool SetProperty(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (Equals(storage, value)) return false;storage = value; OnPropertyChanged(propertyName); return true;
}
public string? UserName
{
get { return _UserName; }
set { SetProperty(ref _UserName, value); }
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer