Procesar CommandParameter a través de WPF MVVM
-
11-12-2019 - |
Pregunta
Estoy bastante begante en WPF. Tengo la casilla de verificación y quiero que todos los cambios de verificación experimenten un comando que obtenga un parámetro isChecked y haga algo de acción.
Tengo el siguiente código en mi archivo xaml:
En mi vismodel tengo el siguiente código:
private ICommand _addSelectedItemsCommand;
public ICommand AddSelectedItemsCommand
{
get
{
if (_addSelectedItemsCommand == null)
{
_addSelectedItemsCommand = new RelayCommand(param => this.AddSelectedItems());
}
return _addSelectedItemsCommand;
}
}
private void AddSelectedItems()
{
Do something...
}
Pero para "hacer" algo ", necesito parámetro isChecked, ¿cómo puedo obtenerlo?
gracias
Solución
Debe usar InvokecommandAction clase.Puede encontrarlo en Expression Blend SDK o simplemente puede agregar este paquete de nuget a su proyecto.
<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>
Otros consejos
En su relevo ViewModel, se ve como
private RelayCommand<string> AddSelectedItemsCommand{get;set;}
y en su código de constructor ViewModel se ve como
AddSelectedItemsCommand=new RelayCommand<string>(AddSelectedItemsMethod);
void AddSelectedItemsMethod(string AddItem)
{
Your Code Goes Here.
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow