Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. WPF .Net Core 6 RelayCommand Problem

WPF .Net Core 6 RelayCommand Problem

Scheduled Pinned Locked Moved WPF
csharpwpfasp-netdotnetdesign
2 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    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
    
    Richard DeemingR 1 Reply Last reply
    0
    • K Kevin Marois

      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
      
      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Looking at the source code for CommunityToolkit.Mvvm.Input.RelayCommand[^], it doesn't use the CommandManager at all. You need to explicitly call the NotifyCanExecuteChanged method[^] instead. NB: That SetProperty 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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups