TreeViewItem with DataTemplateSelector

Aug 12, 2010 at 11:49 AM

Hello,

Can you tell me if it's possible to get services automatically injected into my view models when they are visualized in a TreeView using a ContentTemplateSelector

Here's a short snippet of relevant XAML:

<HierarchicalDataTemplate
x:Key="EntryItemTemplate" 
    DataType="{x:Type vm:EntryViewModel}"
    ItemsSource="{Binding Subentries}"
    ItemTemplate="{StaticResource EntryItemTemplate}"
    ItemContainerStyle="{StaticResource EntryTreeViewItemStyle}">    
    <Border x:Name="border" Width="36" Height="36" BorderBrush="#00000000" BorderThickness="2">
    <Viewbox x:Name="viewbox" >
         <ContentControl Content="{Binding}" ContentTemplateSelector="{Binding DataContext.EntryDataTemplateSelector, ElementName=entriesTreeView}"/>
        </Viewbox>
    </Border>
</HierarchicalDataTemplate>

ContentTemplateSelector provides UserControls for different types of entries (which are entry view models created by a parent view model) and they are correctly shown in the tree. Imported properties of these view models are never injected though. How can I 'help' MEFedMVVM know that it should import services to these view models?

Thank you very much for creating and sharing MEFedMVVM! This is my first issue with it so far and I think overall it is a great library!

    - Jussi

Coordinator
Aug 12, 2010 at 11:59 AM

Yes you can... you can use what I call DataContextAwareServices..... basically you export the viewmodel like this [ExportViewModel("myVM", true)]. passign true as a parameter will make the ViewModel DataContextAware which means that instead of MEF creating the ViewModel you create the ViewModel instance but MEFedMVVM will track that ViewModel and Satisfy its imports...

the imports must be done on properties so you cannot use an Importing Constructor.... i.e [Import]public IMyService Service { get; set; }

There is a sample in the samples folder called DataContextAwareViewModels that shows this.... you can also read more on this here >> http://marlongrech.wordpress.com/2010/05/23/mefedmvvm-v1-0-explained/ (there is a section  DataContextAware ViewModels

Aug 12, 2010 at 12:34 PM
Edited Aug 12, 2010 at 12:37 PM

Sorry, I forgot to mention I was already marking the ViewModels as DataContextAware and had the Imports defined for properties. I also had checked the sample for more information, but wasn't able to find the reason why a view model inside my ContentControl and using a DataTemplateSelector is working differently than the ItemsControl in the example.

The problem remains, do you have any other hints as to how the DataTemplates from a DataTemplateSelector would allow importing services into the EntryViewModels?

Aug 12, 2010 at 12:43 PM

One difference between the example and my app is that my EntryViewModels are created at runtime depending on user actions. That is, none of the entry view models are created when the application and views are initially loaded. Could that be the crucial difference?

Coordinator
Aug 12, 2010 at 12:44 PM

In the DataTemplate do not forget to put the ViewModelLocator.ViewModel attached property otherwise MEFedMVVM cannot track the ViewModel.

Aug 12, 2010 at 12:57 PM

Ugh, stupid of me, that was the problem. I hadn't used ViewModelLocator.ViewModel on all of my Templates and I was testing with one that didn't have it. Thank you very much for helping me out!

Coordinator
Aug 12, 2010 at 1:01 PM
No problemo :)