Ofir
Ofir

Reputation: 5319

Process CommandParameter via WPF MVVM

I`m quite begginer at WPF. I have checkBox and I want that every check changes will excecute a command that gets IsChecked parameter and do some action.

I have the next code in my XAML file:

At my viewModel I have the next code:

    private ICommand _addSelectedItemsCommand;
    public ICommand AddSelectedItemsCommand
    {
        get
        {
            if (_addSelectedItemsCommand == null)
            {
                _addSelectedItemsCommand = new RelayCommand(param => this.AddSelectedItems());
            }
            return _addSelectedItemsCommand;
        }
    }


    private void AddSelectedItems()
    {
        Do something...
    }

But for "Do somthing" I need IsChecked parameter, How can i get it?

Thanks

Upvotes: 1

Views: 605

Answers (2)

Dhaval Patel
Dhaval Patel

Reputation: 7601

In Your ViewModel RelayCommand Look Like

private RelayCommand<string> AddSelectedItemsCommand{get;set;}

And in your ViewModel Constructor code look like

AddSelectedItemsCommand=new RelayCommand<string>(AddSelectedItemsMethod);


void AddSelectedItemsMethod(string AddItem)
{
 Your Code Goes Here.
  }

Upvotes: 1

SHSE
SHSE

Reputation: 2433

You should use InvokeCommandAction class. You can find it in Expression Blend SDK or you can simply add this NuGet package to your project.

<CheckBox
  xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
  xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="Checked">
      <ei:InvokeCommandAction Command="{Binding AddSelectedItemsCommand}" CommandParameter="..." />
    </i:EventTrigger>
  </i:Interaction.Triggers>
</CheckBox>

Upvotes: 0

Related Questions