How can I get the CurrentItem of CollectionViewSource in SL ?

How can I get the CurrentItem of CollectionViewSource in SL ?

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


HK.Lee posted on Tuesday, October 05, 2010

Hi all,

I'm using CollectionViewSource as in the MVVM Video series as such;

<CollectionViewSource x:Key="basicDataLayoutViewModelViewSource" d:DesignSource="{d:DesignInstance my:BasicDataLayoutViewModel, CreateList=True}" />

.

<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource basicDataLayoutViewModelViewSource}}">

My controller class knows view , but don't know how  to extract  CurrentItem of  CollectionViewSource,which is actually binded viewmodel from known view.  It looks simple at first but makes me scratching my head.

Thank you in advance.

HK.lee

RockfordLhotka replied on Tuesday, October 05, 2010

In XAML it is {Binding Path=CurrentItem} or (WPF only) {Binding Path=/}.

In code it is harder if I remember right, as you need to cast the CVS to an underlying interface type to get at the CurrentItem property.

But your viewmodel should never contain such code - because that would create a strongly typed awareness of the view from the viewmodel - bad!

So you are better off having a property on your viewmodel called CurrentItem (or something like that), and binding it to the current item using a binding expression in the XAML.

HK.Lee replied on Wednesday, October 06, 2010

Thank you for your reply, Rocky.

As I'm using MEF in this case, my controller class  can grab related viewmodel from active view in region and can dynamically set CanXXX state of toolbar button.  I think I have to understand deeper regarding collectionviewsource and currentitem.  

Thank you.

HK.Lee 

Copyright (c) Marimer LLC