This project has moved. For the latest updates, please go here.

$datacontext parameter always returns null in action binding

Oct 28, 2015 at 11:20 AM
When using convention to bind a IHaveDatacontext winform view element to a viewmodel method with ($DataContext) parameter, the framework always returns null for $DataContext. The other parameters($event, $ExecutionContext, ... seem to be returning the expected values). You can try the Simpleparameter sample project in the Framework.
Coordinator
Oct 28, 2015 at 11:31 PM
Edited Oct 28, 2015 at 11:32 PM
Hi,

You are right: $datacontext always returns null, because the control it is attached to doesn't implement IHaveDataContext. The control in question is a Button or a ToolStripButton or a ToolStripMenuItem. The view containing these controls implements IHaveDataContext, but the contained controls themselves, they don't implement this interface.

You must keep in mind this project is a migration from WPF where things are a lot different and all elements have a data context natively (there is no IHaveDataContext interface).

The point here is about usefulness of $datacontext parameter. There are several options:
  1. Keep it as it is.
  2. Treat the $datacontext parameter as if it refers to the containing view - in that case, maybe it should be rename to $viewdatacontext.
  3. Return the nearest ancestor with a non null $datacontext.
Solutions 2 and 3 seem pointless since the view is already available using the $view parameter.
Solution 1 is useless and was included just for compatibility. Do you think it's best to remove it altogether?
Oct 29, 2015 at 1:37 PM
Hi Tiago,

Thank you for the explanation. In fact I came to the same conclusion with you after thinking back.
I think the option has to remian to maintain compatibility with WPF. Use option 3 or one can derive from a Button, a ToolStripButton or a ToolStripMenuItem and implement IHaveDatacontext (returning the DataContext of the host View like in option 3)
Coordinator
Oct 29, 2015 at 2:47 PM
Hi,

I'm sorry but I don't quite understand what you are proposing. Can you rephrase it please?