TriggerAction MethodParameter?

TriggerAction MethodParameter?

Old forum URL: forums.lhotka.net/forums/t/9679.aspx


Dane posted on Tuesday, October 26, 2010

Rocky:  I read your response to a post on TriggerAction MethoParameter where you stated that MethodParameter was really for last ditch or fringe operation and the typically you'd want to bind UI properties to your ViewModel instead.  I'm curious how you would suggest handling cases like the following.  Say you have a DataGrid who's ItemSource was bound to a ViewModel where the Model is a read-only list.  In addition you have a button who's Click event is associated with a TriggerAction and a MethodName from the ViewModel.  This is all well and good until the method needs the item currently selected in the DataGrid.  The ViewModel has no reference to the DataGrid and you cannot bind directly to its SelectedItem property.  The best way I've found to handle this seems to be to use the MethodParameter property on the TriggerAction (which works).  Situations similar to this come up all the time and I was wondering how you recommend handling them.

Thanks

RockfordLhotka replied on Tuesday, October 26, 2010

The way you are supposed to do this is to bind the UI control's SelectedItem property to a property of the viewmodel. Which is fine for a lot of controls, but some controls don't have a SelectedItem property you can bind to the viewmodel...

That is exactly why MethodParameter exists - as a workaround for poorly designed UI controls (at least from an MVVM perspective).

Moreover, because some controls have "stubborn" properties (like the Silverlight ListBox.SelectedItems) that don't bind correctly, You can tell TriggerAction to rebind MethodParameter immediately before invoking the method, so you can actually bind to those properties. Usually that's not necessary, but it does happen...

 

Dane replied on Tuesday, October 26, 2010

Excellent.  Thanks for the reply. Your previous post on the subject lead me to wonder if I'd missed something significant for such situations.

Copyright (c) Marimer LLC