Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6781409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:37:14+00:00 2026-05-26T16:37:14+00:00

In my wpf application the main view has 5 tabs with 5 different usercontrols

  • 0

In my wpf application the main view has 5 tabs with 5 different usercontrols , since the user controls are not related to each other, I have created 5 different view models (apart from the main viewmodel).

I thought of having a List or dictionary to have the list of usercontrols and its viewmodels,
Now, I would like to bind the tabitems with the list of usercontrols and assign the datacontexts, but since the list or dictionary can be changed, I dont find a way to bind the usercontrols to the tabitems.

For example, If I have a single tab which will be associated with a usercontrol I can assign

 tab1View tview=new tab1View();
 tview.DataContext= new tab1ViewModel();
 tab1.Content=tview;

But how can I do the same from a list which has the reference of the view and viewmodels of the usercontrols?

Please teach me a best way to achieve this.

**Answer: **

I got the answer for what I need.
First, Generic type collection of the view models should be created
C# – Multiple generic types in one list

public abstract class Metadata
{
}

public class Metadata<DataType> : MetaData where DataType : class
{
private DataType mDataType;
}
List<Metadata> metadataObjects;
metadataObjects.Add(new Metadata<tab1ViewModel>());
metadataObjects.Add(new Metadata<tab2ViewModel>());

Then create a DataTemplate selector if multiple views are to be be referenced with same viewmodel or just apply the DataTemplate

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:37:15+00:00Added an answer on May 26, 2026 at 4:37 pm

    There are a few ways to handle this, though I’d look at using frameworks to help you with MVVM. I myself promote Prism.

    View Injection


    View Discovery


    DataTemplates – Sample

    With DataTemplates you’re defining in XAML (or in code, but XAML is more likely) which view to “automagically” apply to a ContentControl based upon the view-model (DataContext).

    Somewhere in the XAML resources:

    <DataTemplate DataType="{x:Type ViewModel:GeneralSettingsViewModel}">
        <View:GeneralSettingsView/>
    </DataTemplate>
    <DataTemplate DataType="{x:Type ViewModel:AdvancedSettingsViewModel}">
        <View:AdvancedSettingsView/>
    </DataTemplate>
    

    Somewhere in the XAML file that has the resources applied to it:

     <TabControl ItemsSource="{Binding MyViewModelCollection}" />
    

    Note: This only works if you have one view-model per DataTemplate in the scoped resource.


    DataTemplateSelector

    If you have a view-model that can be applied to multiple views and you determine those views through additional logic, you would want to use a DataTemplateSelector. Here is an example:

    Somewhere in the XAML resources:

    <!-- Possible collision because the DataType is of the same type -->
    <DataTemplate x:Key="GeneralSettingsTemplate"
                  DataType="{x:Type ViewModel:SettingsViewModel}">
        <View:GeneralSettingsView/>
    </DataTemplate>
    <DataTemplate x:Key="AdvancedSettingsTemplate"
                  DataType="{x:Type ViewModel:SettingsViewModel}">
        <View:AdvancedSettingsView/>
    </DataTemplate>
    <local:SettingsDataTemplateSelector x:Key="SettingsTemplateSelector"
        GeneralSettingsTemplate="{StaticResource GeneralSettingsTemplate}"
        AdvancedSettingsTemplate="{StaticResource AdvancedSettingsTemplate}" />
    

    Somewhere in the XAML file that has the resources applied to it:

    <TabControl ItemsSource="{Binding MyViewModelCollection}"
                ItemTemplateSelector="{StaticResource SettingsTemplateSelector}" />
    

    SettingsTemplateSelector.cs:

    public class SettingsDataTemplateSelector : DataTemplateSelector
    {
        public DataTemplate GeneralSettingsTemplate { get; set; }
        public DataTemplate AdvancedSettingsTemplate { get; set; }
    
        public override DataTemplate SelectTemplate(Object item,
            DependencyObject container)
        {
            var vm = item as SettingsViewModel;
    
            if (vm == null) return base.SelectTemplate(item, container);
    
            if (vm.IsAdvanced)
            {
                return AdvancedSettingsTemplate;
            }
    
            return GeneralSettingsTemplate;
        }
    }
    

    MSDN: Prism Navigation – http://msdn.microsoft.com/en-us/library/gg430861(v=PandP.40).aspx
    This covers Prism Regions as well as other parts of navigation.

    MSND: View Discovery vs View Injection – http://msdn.microsoft.com/en-us/library/ff921075(v=pandp.20).aspx
    This section covers the differences of View Discovery and View Injection and when to use each.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have WPF Application where I have One main form and other user controls
I'm trying to learn how to design an application which has several different user
I have a wpf application in mvvm pattern. In main view, I have few
I'm working on a WPF application that has no main window (it runs in
I have a Main WPF application and other modules and I am using PRISM
I have a WPF application with the main Window class called MainWindow. Since I
In a WPF application the main window has a content control to which one
I have a WPF application with Main Window which is not a tool window
I wrote little WPF application with 2 threads - main thread is GUI thread
Doing my first MVVM WPF application. I expected to see a Main() method in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.