Assign delegate to DelegateCommand

Topics: Prism v2 - WPF 3.5
Mar 18, 2009 at 12:53 PM
Edited Mar 18, 2009 at 12:59 PM
Hello,

i got 2 delegates which presents my CreateCommand and CanCreateCommand functions. (I "loaded" the 2 Methodes with Reflection into a Dictionary<string,Delegate> List)

The DelegateCommand expects DelegateCommand(System.Action<object>,System.Func<object,bool>). When i assign my delegate to the DelegateCommand i get the InvalidArguments Error.
Im wondering why i cant assign my delegate to the DelegateCommand. Is it possibel to assign delegates to the DelegateCommand?

best regards
Mar 18, 2009 at 7:22 PM

Hi

 

Yes, as the documentation explains “the DelegateCommand allows delegating the commanding logic instead of requiring a handler in the code-behind. It uses a delegate as the method of invoking a target handling method.”

 

An example of using delegate commands can be checked out in the Reference Implementation. The “+” and “-” buttons in the position view use delegate commands. The functionality is stored in the OrdersController class (OnBuy/SellExecuted methods). The full namespace of that class is: StockTraderRI.Modules.Position.Controllers. Meanwhile, the buttons binded to those commands are in the PositionSummaryView (full namespace StockTraderRI.Modules.Position.PositionSummary).

 

If you are binding a method that you don’t know at compile-time (and thus you are using Reflection), you should cast the Delegate to an Action<object>. For it to work, the signature of the method should be of the that type. For example:

void MyMethod(object argument) { }

 

For more information about Delegate Commands you can check the following topic from the documentation:

·         Commands

 

Please let me know if this helps.

 

Damian Schenkelman

http://blogs.southworks.net/dschenkelman

Mar 24, 2009 at 8:37 AM
Hi,

sry for the late reply. When trying to cast the delegate to Action<object>, i will get a InvalidArguments Error. I will view soon as possibel the reference which you told me.

best regards