Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF command binding: CanExecute parameter

Tags:

binding

wpf

Is the parameter passed to the CanExecute method of bound commands the CommandParameter specified in the binding control? If not, where does it come from?

like image 871
Commanding Newbie Avatar asked Aug 19 '11 18:08

Commanding Newbie


People also ask

How do you pass a command parameter in WPF MVVM?

A parameter can be passed through the "CommandParameter" property. Once the button is clicked the selected address value is passed to the ICommand. Execute method. The CommandParameter is sent to both CanExecute and Execute events.

What is command binding in WPF?

The command is the action to be executed. The command source is the object which invokes the command. The command target is the object that the command is being executed on. The command binding is the object which maps the command logic to the command.

What is the use of ICommand in WPF?

ICommand is an interface between the Presentation & the BusinessLogic layer. Whenever any button is pressed on the screen, XAML has its code-behind ButtonClick event. But in MVVM architecture, there is no room for code-behind to keep the application loosely coupled, so ICommand was introduced.

What is ICommand C#?

The ICommand interface is the code contract for commands that are written in . NET for Windows Runtime apps. These commands provide the commanding behavior for UI elements such as a Windows Runtime XAML Button and in particular an AppBarButton .


2 Answers

The CommandParameter is sent to both the CanExecute and Execute(d) events.

like image 165
Alex Curtis Avatar answered Sep 22 '22 20:09

Alex Curtis


Alex Curtis is right:

public bool CanExecute(object parameter)
public void Execute(object parameter)

Use both the object that was set as CommandParameter for the command.

This is also good to know when calling ICommand's OnCanExecuteChanged(EventArgs e) because this method won't let you pass any parameter. So CanExecute must use the CommandParameter previously set.

like image 27
Knasterbax Avatar answered Sep 23 '22 20:09

Knasterbax