none
How do i use RelayCommand with multiparameters via MVVM

    Question

  • I have just started to learn about MVVM Light .
    It's strong and MVVM - oriented but i got a problem with it.
    When i create a command CheckUser via RelayCommand on my ViewModel,i just found the RelayCommand<T> with only one parameter
    I want something like CheckUser=new RelayCommand<string,string>((user,pass)=>....)
    How can i do this with RelayCommand multi param or some method to resolve my problem.

    Sunday, April 24, 2011 4:57 AM

Answers

  • You don't disturb me :-). Here is an example. First the view model:

    public class MyViewModel : ViewModelBase
    {
        private string _userName;
        public string UserName
        {
            get
            {
                return _userName;
            }
            set
            {
                _userName = value;
                RaisePropertyChanged("UserName");
            }
        }
    
        private string _password;
        public string Password
        {
            get
            {
                return _password;
            }
            set
            {
                _password = value;
                RaisePropertyChanged("Password");
            }
        }
    
        private bool _isLoggedIn;
        public bool IsLoggedIn
        {
            get
            {
                return _isLoggedIn;
            }
            set
            {
                _isLoggedIn = value;
                RaisePropertyChanged("IsLoggedIn");
            }
        }
    
        public RelayCommand AuthenticateCommand
        {
            get;
            private set;
        }
    
        public MyViewModel()
        {
            AuthenticateCommand = new RelayCommand(Authenticate);
        }
    
        private void Authenticate()
        {
            if (string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(Password))
            {
                return;
            }
    
            if (UserName == "Fritz" && Password == "Secret")
            {
                IsLoggedIn = true;
            }
        }
    }


    As you can see it has a command with no parameters at all, and three properties. Two for the user name and password, and one to indicate if the user is logged in or not. The authentication uses a hard-coded user name/password combination for simplicity.

    Now the XAML:

    <StackPanel Width="300">
        <TextBlock Text="User name:" />
        <TextBox Text="{Binding UserName, Mode=TwoWay}" />
        <TextBlock Text="Password:" />            
        <PasswordBox Password="{Binding Password, Mode=TwoWay}" />
        <Button Content="Login"
                Command="{Binding AuthenticateCommand}" />
        <TextBlock Text="Is logged in:" />
        <TextBlock Text="{Binding IsLoggedIn}" />
    </StackPanel>


    Both the user name and password boxes are set up with a two-way binding, so the values are written back to the view model. The button uses the AuthenticateCommand property (the relay command), and for demonstration the last text block shows the IsLoggedIn property.

    If you now set up the page like this:

    public MainPage()
    {
        InitializeComponent();
    
        DataContext = new MyViewModel();
    }


    ... and run the application, you can see that the "Is logged in" label only switches to "true" if you enter the correct user name and password. 

    We have successfully used multiple "arguments" in the relay command (the user name and password) just by making use of Silverlight's data binding features. In turn, when the authentication was successful, we used the IsLoggedIn property to enable the UI to change, once again using data binding.

    Hope this helps.



    Monday, April 25, 2011 1:47 PM

All replies

  • Hi. You should set up some bindings to properties of your view model for these values. E.g. bind the text boxes, or whereever these values come from, to properties. Then, when you execute the command, you don't need to pass any parameters into it at all, because you can simply use the property values of your view model.


    Sunday, April 24, 2011 5:09 AM
  • Thanks MisterGoodCat,

    But i don't know much your ideal (because i just a beginner of MVVM :( )

    Don't have any Generic RelayCommand<T,T..> for multiparam ?

    So if i want to implement some function like as Insert or receive the result of the command (something like true if user log in successful), what should i do?


    Sunday, April 24, 2011 5:50 AM
  • No, there are no such signatures of relay command that I know of. 

    So if i want to implement some function like as Insert or receive the result of the command (something like true if user log in successful), what should i do?

    This wouldn't work anyway, because those operations - assuming they are performed by a web service - are async in their nature in Silverlight. That means that you couldn't return any results from the command method anyway.

    What you do for these scenarios is data binding. E.g.:

    • Use two text boxes with a two-way data binding set up. The result of this is that both values will be available in your view model through properties.
    • Wire up a relay command e.g. for a button. When the user clicks on the button your command method in your view model is executed. In this method you have access to the above mentioned property values (that is how you "pass in" multiple "arguments" to the command).
    • When the invoked operation of the command has finished, for example an async web service method, update another property in your view model (e.g. set "IsLoggedIn" to the corresponding result).
    • In your view, use the "IsLoggedIn" property to react and adjust your UI accordingly.


    Sunday, April 24, 2011 6:09 AM
  • I still confuse about this.

    Can you give me some example for this ?

    Sorry if i disturb you.

    Thanks so much MisterGoodCat

    Sunday, April 24, 2011 6:41 AM
  • I still need to help resovle my problem.

    Plz

    Monday, April 25, 2011 11:21 AM
  • Hi,

    Just borrow Prism assembly then you can use that.

    Take a look at Prism website.

    It is easily to use than Relay Command.

    Monday, April 25, 2011 11:23 AM
  • You don't disturb me :-). Here is an example. First the view model:

    public class MyViewModel : ViewModelBase
    {
        private string _userName;
        public string UserName
        {
            get
            {
                return _userName;
            }
            set
            {
                _userName = value;
                RaisePropertyChanged("UserName");
            }
        }
    
        private string _password;
        public string Password
        {
            get
            {
                return _password;
            }
            set
            {
                _password = value;
                RaisePropertyChanged("Password");
            }
        }
    
        private bool _isLoggedIn;
        public bool IsLoggedIn
        {
            get
            {
                return _isLoggedIn;
            }
            set
            {
                _isLoggedIn = value;
                RaisePropertyChanged("IsLoggedIn");
            }
        }
    
        public RelayCommand AuthenticateCommand
        {
            get;
            private set;
        }
    
        public MyViewModel()
        {
            AuthenticateCommand = new RelayCommand(Authenticate);
        }
    
        private void Authenticate()
        {
            if (string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(Password))
            {
                return;
            }
    
            if (UserName == "Fritz" && Password == "Secret")
            {
                IsLoggedIn = true;
            }
        }
    }


    As you can see it has a command with no parameters at all, and three properties. Two for the user name and password, and one to indicate if the user is logged in or not. The authentication uses a hard-coded user name/password combination for simplicity.

    Now the XAML:

    <StackPanel Width="300">
        <TextBlock Text="User name:" />
        <TextBox Text="{Binding UserName, Mode=TwoWay}" />
        <TextBlock Text="Password:" />            
        <PasswordBox Password="{Binding Password, Mode=TwoWay}" />
        <Button Content="Login"
                Command="{Binding AuthenticateCommand}" />
        <TextBlock Text="Is logged in:" />
        <TextBlock Text="{Binding IsLoggedIn}" />
    </StackPanel>


    Both the user name and password boxes are set up with a two-way binding, so the values are written back to the view model. The button uses the AuthenticateCommand property (the relay command), and for demonstration the last text block shows the IsLoggedIn property.

    If you now set up the page like this:

    public MainPage()
    {
        InitializeComponent();
    
        DataContext = new MyViewModel();
    }


    ... and run the application, you can see that the "Is logged in" label only switches to "true" if you enter the correct user name and password. 

    We have successfully used multiple "arguments" in the relay command (the user name and password) just by making use of Silverlight's data binding features. In turn, when the authentication was successful, we used the IsLoggedIn property to enable the UI to change, once again using data binding.

    Hope this helps.



    Monday, April 25, 2011 1:47 PM
  • So cool MisterGoodcat.

    A ton of thanks for you :D .

    Problem has been resolved.

    Monday, April 25, 2011 9:27 PM
  • Yes, It is a very gud example. But i want the same thing in grid.


    I have main grid and in its detail row another grid is there.

    When user clicks on the button of the inner grid then i want to pass the two param using the commandparam of command .


    How i can do the same?

    Friday, June 03, 2011 8:01 AM
  • Like I said, there's no signature that would allow that.


    Friday, June 03, 2011 10:57 AM