views:

553

answers:

1

Hi, 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.

+11  A: 

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));
Matt Hamilton
Thank You Very Much, its working greatly.Thanks again.
Naresh Goradara
Naresh - don't forget to accept this answer if it solved your problem. Having a 0% accept rate won't help you get answers in future.
Matt Hamilton