Binding to ViewModel from within DataTemplate

Nov 23, 2010 at 3:41 PM

Hi,

I have a list browse screen and a corresponding view model (ItemListViewModel) containing a list of items. The items have a DataTemplate. The question is how can i reference ItemListViewModel from within the item DataTemplate??

Ive tried the following binding without success. 


CM:Bind.Model="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ViewModels:ItemListViewModel}}}

 

Im trying to call a method on the ItemListViewModel.  Any assistance would be greatly appreciated.

 

Regards

David

Nov 23, 2010 at 5:01 PM

If I have not misunderstood your question, I suppose that you can do something like

CM:Bind.Model="{Binding Path=DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}"

Note that if the ListView view-model was set using Bind.Model, you can even decide to bind to that property

CM:Bind.Model="{Binding Path=(CM:Bind.Model), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}"

 

 

Nov 24, 2010 at 8:56 AM

Yeah that makes sense. Thanks for the reply, i'll give that a go.