Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9170805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:04:40+00:00 2026-06-17T16:04:40+00:00

I want to implement MVVM patter to a registeration page like this: The Page

  • 0

I want to implement MVVM patter to a registeration page like this:

The Page has text boxes for username, email and password.

I want to bind the Register Button to a command using ICommand and DelegateCommand pattern.

The problem is that I want the button to be disabled if the textboxes are empty and enabled if they have text.

My Model is:

public class User
    {
        public string UserName { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
    }

My ViewModel:

public class UserViewModel:INotifyPropertyChanged
    {
        private User user;
        public UserViewModel()
        {
            user = new User();
        }
#region Properties
.
.
.
#endregion
public ICommand RegisterCommand
        {
            get
            {
                return new DelegateCommand(Register,CanRegister);
            }
        }

        private void Register(object parameter)
        {
            //TODO call the web service
        }

        private bool CanRegister(object parameter)
        {
            return (!string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(Password));
        }
}

My DelegateCommand Implementation:

public class DelegateCommand:ICommand
    {
        //Delegate to the action that the command executes
        private Action<object> _executeAction;
        //Delegate to the function that check if the command can be executed or not
        private Func<object, bool> _canExecute;

        public bool canExecuteCache;

        public DelegateCommand(Action<object> executeAction):this(executeAction,null)
        {

        }

        public DelegateCommand(Action<object> action, Func<object, bool> canExecute)
        {
            this._executeAction = action;
            this._canExecute = canExecute;
        }

        //interface method, called when CanExecuteChanged event handler is fired
        public bool CanExecute(object parameter)
        {
            //true by default (in case _canExecute is null)
            bool result = true;
            Func<object, bool> canExecuteHandler = this._canExecute;
            if (canExecuteHandler != null)
            {
                result = canExecuteHandler(parameter);
            }

            return result;
        }

        //Event handler that the controld subscribe to 
        public event EventHandler CanExecuteChanged;


        //interface method
        public void Execute(object parameter)
        {
            _executeAction(parameter);
        }


        //rause the CanExecuteChanged event handler manually
        public void RaiseCanExecuteChanged()
        {
            EventHandler handler = this.CanExecuteChanged;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }

        }



    }

and My View:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <TextBlock Grid.Row="0" Text="Username:"/>
            <TextBox Grid.Row="1" Name="txtUserName" Text="{Binding UserName, Mode=TwoWay}" HorizontalAlignment="Stretch"/>
            <TextBlock Grid.Row="2" Text="Password:" HorizontalAlignment="Stretch" />
            <TextBox Grid.Row="3" Name="txtPassword" Text="{Binding Password, Mode=TwoWay}"/>
            <Button Grid.Row="4" Content="Register" Command="{Binding RegisterCommand }"   />
        </Grid>

What I want to achieve is to make the button disabled untill the user enters information in each TextBox

how can this be done ?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:04:41+00:00Added an answer on June 17, 2026 at 4:04 pm

    One thing first: Returning a new DelegateCommand every time the property is accessed will limit your ability to call the RaiseCanExecuteChanged() method as you won’t have a reference to the same command as is bound.

    So change your ViewModel to be something like this:

    public class UserViewModel : INotifyPropertyChanged
    {
       private User user;
       public UserViewModel()
       {
          user = new User();
          RegisterCommand = new DelegateCommand(Register,CanRegister);
       }
    
       public DelegateCommand RegisterCommand {get; private set;}
    
       private void Register(object parameter)
       {
          //TODO call the web service
       }
    
       private bool CanRegister(object parameter)
       {
          return (!string.IsNullOrEmpty(UserName) && 
                  !string.IsNullOrEmpty(Password));
       }
    }
    

    The reason you can have the RegisterCommand property as private set with no PropertyChanged call as it will be instantiated before the binding occurs and doesn’t need to change.

    Assuming the form of the properties UserName and Password trigger the PropertyChanged event, you can just call the RaiseCanExecuteChanged() method on the RegisterCommand when they change.

    Eg.

    private string _userName;
    public string UserName
    {
        get { return _userName; }
        set
        {
            if(_userName == value)
                return;
    
            _userName = value;
            RaisePropertyChanged("UserName");
    
            RegisterCommand.RaiseCanExecuteChanged();
        }
    }
    

    this will force the CanExecute method to re-evaluated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a login page where the users enters username/password then a
i starter in jqgrid, i want implement inline edit in jqgrid i have this
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
Microsoft has announce that WindowsLiveID become a OpenID provider . I want implement it
I am using a standard wpf/mvvm application where i bind combo boxes to collections
I can implement Mvvm with Knockout.js. But I want to use it with cross
I'd like to implement the ViewModel part of WPF's MVVM pattern without referencing WPF
I want implement in my software solution an VBA editor but in c# 3.0.
I want to implement a simple script to do some boring housekeeping on my
I want to implement charting in SWT and I want to use GC.transform to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.