Receiving CommandParameter Value in MVVM
Asked Answered
M

1

14

I am binding my command like:

<Button Command="{Binding NextCommand}" 
    CommandParameter="Hello" 
    Content="Next" /> 

Here, I also bind its CommandParameter property, now how to fetch its value from NextCommand.

public ICommand NextCommand
    {
        get
        {
            if (_nextCommand == null)
            {
                _nextCommand = new RelayCommand(
                    param => this.DisplayNextPageRecords()
                    //param => true
                    );
            }
            return _nextCommand;
        }
    }

Its function definition:

public ObservableCollection<PhonesViewModel> DisplayNextPageRecords()
    {

            //How to fetch CommandParameter value which is set by 
            //value "Hello" at xaml. I want here "Hello"
            PageNumber++;
            CreatePhones();
            return this.AllPhones;

    }

How to fetch CommandParameter value?

Thanks in advance.

Malvoisie answered 14/10, 2009 at 8:51 Comment(0)
B
33

Change your method definition:

public ObservableCollection<PhonesViewModel> DisplayNextPageRecords(object o)
{
    // the method's parameter "o" now contains "Hello"
    PageNumber++;
    CreatePhones();
    return this.AllPhones;
}

See how when you create your RelayCommand, its "Execute" lambda takes a parameter? Pass that into your method:

_nextCommand = new RelayCommand(param => this.DisplayNextPageRecords(param));
Broomfield answered 14/10, 2009 at 9:4 Comment(1)
and then you can use like _nextCommand = new RelayCommand(this.DisplayNextPageRecords);Fingertip

© 2022 - 2024 — McMap. All rights reserved.