如何在MVVM中向命令传递参数

本文关键字:命令 参数 MVVM | 更新日期: 2023-09-27 17:51:22

视图:<Button Command="{Binding GetChangeLogCommand}"

ViewModel: GetChangeLogCommand = new DelegateCommand(GetChangeLog);

方法:

public ICommand GetChangeLogCommand { get; set; }
private async void GetChangeLog(bool isLatest)
{
    // code goes here . . .
}

我将多次使用GetChangeLogCommand并传递布尔参数是否可能使用MVVM对不起,我是新的

如何在MVVM中向命令传递参数

如果你特别想把button元素传递给viewmodel,你可以使用下面的代码:

 CommandParameter="{Binding ElementName=YourButtonName}"

甚至可以将整个窗口、用户控件或特定列表或列表项传递给视图模型

 CommandParameter="{Binding ElementName=YourWindow}"     
 CommandParameter="{Binding ElementName=Control}"