Question

Je suis assez mbrgyer chez WPF. J'ai cocher la case à cocher et je veux que chaque chèque change excactive une commande qui obtient un paramètre ischecked et faire une action.

J'ai le code suivant dans mon fichier XAML:

à ma viewModel, j'ai le code suivant:

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


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

Mais pour "faire quelque chose", j'ai besoin de paramètres ischecked, comment puis-je l'obtenir?

merci

Était-ce utile?

La solution

Vous devez utiliser InvokeCommandaction classe.Vous pouvez le trouver dans Expression Blend SDK ou vous pouvez simplement ajouter ce package Nuget à votre projet.

<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>

Autres conseils

Dans votre point de vue du relaisModelCommand ressemble à

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

et dans votre code de constructeur de menuGModel ressemblent à

AddSelectedItemsCommand=new RelayCommand<string>(AddSelectedItemsMethod);


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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top