MVVM Video Series - TriggerAction DataContext CurrentItem

MVVM Video Series - TriggerAction DataContext CurrentItem

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


Jaans posted on Tuesday, August 10, 2010

Going through the MVVM Video series, I see Rocky use the "CurrentItem" property of the CollectionViewSource when binding the DataContext for the trigger action.

I've generally used the implicit binding expression below when referencing the current bound to item:

<my1:TriggerAction ... MethodName="Save" DataContext="{Binding}" />

instead of:

<my1:TriggerAction ... MethodName="Save" DataContext="{Binding Path=CurrentItem}" />

I guess this has more to do with CollectionViewSource, but other binding sources could use different property names for their "current item" (other than CurrentItem).

So my thinking is that I'm typing less and letting Silverlight do more heavy lifting this way, are there caveats here that could pose a problem? Are the two techniques truely the same thing?

Jaans

RockfordLhotka replied on Tuesday, August 10, 2010

TriggerAction needs to bind to the viewmodel object.

If the DataContext is a CollectionViewSource you need to set up the binding so the CVS isn't the target, but rather the current item is the target.

Sure, other resource/container/manager types may use different schemes - that's quite possible.

But if you use the drag-and-drop designer in VS10 you will have a CVS, because that's what it creates - and for the video series I specifically chose to use the designer.

Jaans replied on Wednesday, August 11, 2010

Makes sense.

Interesting thing is that it seems to work without specifying the "CurrentItem" property, and that's what you can do in the designer also.

When you set the DataContext for a trigger action using the VS2010 Designer, the binding editor allows you to set Source to the CVS and then you can set the "Path" property to <none> (instead of CurrentItem).

It's a bit weird though... as it feels more instinctive to go with CurrentItem because this is clearly the CVS we've bound to and that the method we are targetting is not on the CVS but on the currently bound item.

Thanks for the feedback.

RockfordLhotka replied on Wednesday, August 11, 2010

I've had trouble with TriggerAction sometimes not working unless I specify CurrentItem. Normally CurrentItem should be the default - a binding expression should transparently just use the current item - but since that hasn't been reliable for me, I use CurrentItem to be safe.

Copyright (c) Marimer LLC