0
votes

The datacontext's are configured in App.xaml (with MVVMLight), but when is one ViewModel one View.

<DataTemplate DataType="{x:Type vm:VMUserControl}">
   <views:UCViewSnow />
</DataTemplate>

I need use the same ViewModel with multiples Views, i added DataContext in codebehind for each View (.xaml.cs) but not working.

DataContext = ViewModel.ViewModelLocator.GetStaticVMuserControl;

I need to add DataContext according to parameters from app.config. I use WPF with framework 4.5.1 with MVVM light from nuget in VS 2015

<DataTemplate DataType="{x:Type vm:VMUserControl}"> 
    <views:UCViewDark /> 
</DataTemplate>

<DataTemplate DataType="{x:Type vm:VMUserControl}">
    <views:UCViewSnow/> 
</DataTemplate>
1
Have you been able to identify any particular indications that it's "not working"? Or do you just have a general sense of foreboding, or malaise?15ee8f99-57ff-4f92-890c-b56153
yes, i can't add two viewmodels in App.xaml with datatemplate. From codebehind in *.xaml.cs not working " <DataTemplate DataType="{x:Type vm:VMUserControl}"> <views:UCViewDark /> </DataTemplate>" <DataTemplate DataType="{x:Type vm:VMUserControl}"> <views:UCViewSnow/> </DataTemplate>quevedod86
Are you having trouble editing the file? Is caps lock on?15ee8f99-57ff-4f92-890c-b56153
No, i want to write code between tags for style. (i don't know it)quevedod86
Are you saying that you're trying to use a style to set a viewmodel? If so, you're creating only one copy of the viewmodel in the style, so that's the only copy you get. Is that what you're getting at?15ee8f99-57ff-4f92-890c-b56153

1 Answers

0
votes

I see couple potential reasons why DataTemplate can didn't work.

1st - probably you didn't bind Content property of your view with your DataContext (vm:VMUserControl).
2nd - you use custom template of content control without ContentPresenter.

For example: App.xaml

...
  <Application.Resources>
      <vm:ViewModelLocator x:Key="Locator" />
  </Application.Resources>
...

In your View

Update: In case if you define two datatemplate for one DataType (viewmodel) you additional should implement and use own ContentTemplateSelector.

<View ...
     DataSource={Binding Source={StaticResource Locator}, Path=YourViewModel}>

    <View.Resource>
        <ResourceDictionar>
               <DataTemplate DataType="{x:Type vm:VMUserControl}" 
                             x:Key="darkDataTemplate"> 
                   <views:UCViewDark /> 
               </DataTemplate>

               <DataTemplate DataType="{x:Type vm:VMUserControl}" 
                             x:Key="snowDataTemplate">
                  <views:UCViewSnow/> 
               </DataTemplate>

              <local:CustomContentTemplateSelector x:Key="yourContentTemplateSelector"
                     FirstDataTemplate={StaticResource snowDataTemplate}
                     SecondDataTemplate={StaticResource darkDataTemplate} />
        </ResourceDictionar>
    </View.Resource>

            <ContentControl Content={Binding} 
                            ContentTemplateSelector={StaticResource yourContentTemplateSelector}/>

CustomContentTemplateSelector.cs

public class CustomContentTemplateSelector:DataTemplateSelector
{
    public DataTemplate FirstDataTemplate {get; set;}
    public DataTemplate SecondDataTemplate {get; set;}

    protected override DataTemplate SelectTemplateCore(object item, DependencyObject container){
    // here you should impelement logic for choose data template 
    // if (condition) 
    //      return FirstDataTemplate
    // else
    //      return SecondDataTemplate
}
}