简体   繁体   English

如何在MVVM中将参数传递给命令

[英]How to Pass parameter to a Command in MVVM

View: <Button Command="{Binding GetChangeLogCommand}" 查看: <Button Command="{Binding GetChangeLogCommand}"

ViewModel: GetChangeLogCommand = new DelegateCommand(GetChangeLog); ViewModel: GetChangeLogCommand = new DelegateCommand(GetChangeLog);

Method: 方法:

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

I'm going to use GetChangeLogCommand multiple time and pass boolean parameter is it possible with MVVM sorry I'm new with this. 我将多次使用GetChangeLogCommand并通过布尔参数传递MVVM是否可以,抱歉,我是新来的。

If you are that particular to pass the button element to viewmodel You can use below code : 如果您特别希望将button元素传递给viewmodel,则可以使用以下代码:

 CommandParameter="{Binding ElementName=YourButtonName}"

You could even pass the entire window, user control, or a particular list or list item to the view model 您甚至可以将整个窗口,用户控件或特定的列表或列表项传递给视图模型。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用MVVM,如何将XamDataGrid的SelectedItems作为参数传递给ContextMenu引发的Command? - Using MVVM, how to pass SelectedItems of a XamDataGrid as parameter to the Command raised by the ContextMenu? 将委托/命令作为参数传递给MVVM中的另一个命令 - To pass a delegate/command as a parameter to another command in MVVM MVVM-Light =>将命令参数和EventArgs传递给Command - MVVM-Light => Pass Command Parameter AND EventArgs to Command 如何使用 MVVM 将 UserControl 参数传递给 ViewModel - How to pass a UserControl parameter to the ViewModel using MVVM 如何在 MVVM Xamarin Forms 中将参数传递给 Singleton - How to pass a parameter to Singleton in MVVM Xamarin Forms 在MVVM中绑定事件并将事件参数作为命令参数传递 - Bind event in MVVM and pass event arguments as command parameter 如何传递按钮的命令参数 - How to pass Command Parameter of Button 如何在没有 mvvm light 的情况下将参数传递给导航页面的视图模型 - How to pass a parameter to the viewmodel of the navigated page without mvvm light 如何在viewmodel(MVVM Model)wpf应用程序中使用命令行参数 - how to use Command line parameter in viewmodel (MVVM Model) wpf application 与使用绑定的ViewModel对象(MVVM)相比,将所选项目作为命令参数传递 - Pass selected item as command parameter vs. using a bound ViewModel Object (MVVM)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM