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 7072295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:47:28+00:00 2026-05-28T05:47:28+00:00

I am learning to use DelgateCommand from Prism…. In my UI, I have my

  • 0

I am learning to use DelgateCommand from Prism….

In my UI, I have my UserName textbox and PasswordBox:

<TextBox Name="_UserNameTextBox" Text="{Binding UserName, Mode=TwoWay}" />

<PasswordBox Name="_PasswordBox"></PasswordBox>

And my Login Button:

<Button Name="button1" Command="{Binding LoginCommand, Mode=TwoWay}" CommandTarget="{Binding ElementName=_UserNameTextBox, Path=Text}">Login</Button>

Then my ViewModel I have:

    string _UserName = string.Empty;
    public string UserName
    {
        get
        {
            return _UserName;
        }
        set
        {
            if (value != _UserName)
            {
                _UserName = value;
                RaisePropertyChanged("UserName");
            }
        }

    }

    //For reference the password
    PasswordBox _PasswordBox { get; set; }


    public DelegateCommand<string> LoginCommand { get; set; }

    public LoginViewModel(PasswordBox passwordBox)
    {
        _PasswordBox = passwordBox;

        LoginCommand = new DelegateCommand<string>(
            (
                //Execute
                (str) =>
                {
                    Login(_PasswordBox.Password);
                }
            ),
                //CanExecute Delgate
                (usr) =>
                {
                    if (string.IsNullOrEmpty(usr) || string.IsNullOrEmpty(_PasswordBox.Password))
                        return false;
                    return true;
                }
            );
    }

I can see my UserName is binding properly and I did pass my PasswordBox as referece in ViewModel constructor. When I execute the application the Button is disabled, so I know is binded to the command.

But I never see the CanExecute delgate that I wrote is being check after I type things in UserName and PasswordBox…. And is never enabled…

So what did I done wrong?

EDIT:

=====

So end result is…this?

string _UserName = string.Empty;
        public string UserName
        {
            get
            {
                return _UserName;
            }
            set
            {
                if (value != _UserName)
                {
                    _UserName = value;
                    RaisePropertyChanged("UserName");
                    LoginCommand.RaiseCanExecuteChanged();
                }
            }

        }

        //For reference the password
        PasswordBox _PasswordBox { get; set; }


        public DelegateCommand<string> LoginCommand { get; set; }

        public LoginViewModel(PasswordBox passwordBox)
        {
            _PasswordBox = passwordBox;
            _PasswordBox.PasswordChanged += delegate(object sender, System.Windows.RoutedEventArgs e)
            {
                LoginCommand.RaiseCanExecuteChanged();
            };
            LoginCommand = new DelegateCommand<string>(
                (
                    (str) =>
                    {
                        Login(_PasswordBox.Password);
                    }
                ),
                    (usr) =>
                    {
                        if (string.IsNullOrEmpty(usr) || string.IsNullOrEmpty(_PasswordBox.Password))                        
                            return false;
                        return true;
                    }
                );
        }
  • 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-05-28T05:47:29+00:00Added an answer on May 28, 2026 at 5:47 am

    Generally speaking, you have to call RaiseCanExecuteChanged whenever the effecting value returned by CanExecute changes. In this specific case you would need to call it whenever the value of the user or password fields changes. But that is exceedingly difficult, because your ViewModel implementation is totally wrong.

    Here’s what you should do instead:

    1. Expose a Username and a Password property inside your ViewModel. You will need to implement the getters and setters explicitly (i.e. it cannot be an automatic property).
    2. From within your view, bind the contents of the username and password input fields to these properties.
    3. Inside the property setters, call LoginCommand.RaiseCanExecuteChanged.

    Here’s what will happen when you do this (let’s pick the password box for an example):

    1. The user types a character inside the password box.
    2. WPF sets the value of LoginViewModel.Password because of the two-way binding.
    3. The password setter calls RaiseCanExecuteChanged, which raises the CanExecuteChanged event on your command.
    4. The submit button (which has subscribed to that event when you bound it to the command) gets notified.
    5. The button calls CanExecute to see if executing the command is now allowed.
    6. Your delegate runs and returns true, so the button activates itself.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been learning to use Emacs for a little while now. So far
I am learning how to use NUnit. I have my main project in it's
I am learning to use sed and have a hypothetical requirement: bash$ cat states
I am learning to use Core Data. I have created a data model etc
I have enjoyed learning to use OpenGL under the context of games programming, and
I am learning to use Processing , and have modified one of the examples
I'm learning to use pointer to copy char array. I have the following code
I am learning to use mahout by starting with a example copied from the
I am learning to use mysql.So, I got some problems. I have three table
I am learning to use R. I have an interest in pulling stock data

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.