Domanda

Sono abbastanza begogner a WPF. Ho la casella di controllo e voglio che ogni modifica dei controlli sposterà un comando che ottiene il parametro isChecked e fai qualche azione.

Ho il codice successivo nel mio file XAML:

Ay My ViewModel ho il codice successivo:

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


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

Ma per "fare qualcosa" ho bisogno di un parametro ischecked, come posso ottenerlo?

Grazie

È stato utile?

Soluzione

Dovresti usare Invikecomandaction classe.Puoi trovarlo in Expression Blend SDK o puoi semplicemente aggiungere Questo pacchetto Nuget al tuo progetto.

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

Altri suggerimenti

Nel tuo ViewModel RelayCommand sembra

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

E nel vostro viewModel il codice costruttore sembra

AddSelectedItemsCommand=new RelayCommand<string>(AddSelectedItemsMethod);


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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top