如何以编程方式使用wpf mvvm参数调用relay命令?

时间:2022-08-25 09:10:47

I know how to invoke relay command without parameter using mvvm pattern, but how to do the same with command with parameter?

我知道如何使用mvvm模式调用不带参数的relay命令,但如何使用带参数的命令执行相同操作?

https://i.stack.imgur.com/o7r5i.jpg
https://i.stack.imgur.com/zNkYR.jpg
https://i.stack.imgur.com/lmw3w.jpg
https://i.stack.imgur.com/iJnF0.jpg

2 个解决方案

#1


0  

If I understand you correctly, your command requires you to pass the TextEditor object in as a parameter, and you'd like to know how to do this in XAML. Since your TextEditor is named XMLView you'd simply bind this to the command parameter;

如果我理解正确,您的命令要求您将TextEditor对象作为参数传递,并且您想知道如何在XAML中执行此操作。由于您的TextEditor被命名为XMLView,您只需将其绑定到命令参数;

<KeyBinding Command="{Binding ValidateXMLCommand}" CommandParameter="{Binding ElementName=XMLView}" Modifiers="Control" Key="V" />

Notice the addition of CommandParameter="{Binding ElementName=XMLView}", this will pass the AvalonEdit TextEditor control instance as a parameter of the command.

注意添加了CommandParameter =“{Binding ElementName = XMLView}”,这将传递AvalonEdit TextEditor控件实例作为命令的参数。

Read more; https://*.com/a/32064646/8520655

阅读更多; https://*.com/a/32064646/8520655

If you instead mean to invoke the RelayCommand from a ViewModel (in normal C#), you'd do the following;

如果你的意思是从ViewModel调用RelayCommand(在普通的C#中),你会做以下事情;

if (ValidateXMLCommand.CanExecute(XMLView))
                ValidateXMLCommand.Execute(XMLView);

Also, please do not post images of code, but rather your code formatted using the code style.

另外,请不要发布代码图像,而是使用代码样式格式化代码。

#2


0  

The control (e.g. Button / MenuItem) that you're binding your relaycommand to will have a CommandParameter property in addition to the Command property.

除了Command属性之外,您绑定relaycommand的控件(例如Button / MenuItem)将具有CommandParameter属性。

See here for an example of usage.

请参阅此处以获取使用示例。

To execute a command from code behind, just call its Invoke() method, with the required parameter.

要从后面的代码执行命令,只需使用所需参数调用其Invoke()方法。

#1


0  

If I understand you correctly, your command requires you to pass the TextEditor object in as a parameter, and you'd like to know how to do this in XAML. Since your TextEditor is named XMLView you'd simply bind this to the command parameter;

如果我理解正确,您的命令要求您将TextEditor对象作为参数传递,并且您想知道如何在XAML中执行此操作。由于您的TextEditor被命名为XMLView,您只需将其绑定到命令参数;

<KeyBinding Command="{Binding ValidateXMLCommand}" CommandParameter="{Binding ElementName=XMLView}" Modifiers="Control" Key="V" />

Notice the addition of CommandParameter="{Binding ElementName=XMLView}", this will pass the AvalonEdit TextEditor control instance as a parameter of the command.

注意添加了CommandParameter =“{Binding ElementName = XMLView}”,这将传递AvalonEdit TextEditor控件实例作为命令的参数。

Read more; https://*.com/a/32064646/8520655

阅读更多; https://*.com/a/32064646/8520655

If you instead mean to invoke the RelayCommand from a ViewModel (in normal C#), you'd do the following;

如果你的意思是从ViewModel调用RelayCommand(在普通的C#中),你会做以下事情;

if (ValidateXMLCommand.CanExecute(XMLView))
                ValidateXMLCommand.Execute(XMLView);

Also, please do not post images of code, but rather your code formatted using the code style.

另外,请不要发布代码图像,而是使用代码样式格式化代码。

#2


0  

The control (e.g. Button / MenuItem) that you're binding your relaycommand to will have a CommandParameter property in addition to the Command property.

除了Command属性之外,您绑定relaycommand的控件(例如Button / MenuItem)将具有CommandParameter属性。

See here for an example of usage.

请参阅此处以获取使用示例。

To execute a command from code behind, just call its Invoke() method, with the required parameter.

要从后面的代码执行命令,只需使用所需参数调用其Invoke()方法。