Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I know this subject has been beaten to death, but no example I have found explains exactly what I need. I created an example application that loads a view model via an ObservableCollection. The main window has a DataTemplate that links the view to it. All is well and good and it works fine. However, I need to switch the main window between entirely different ViewModels and their respective views arbitrary at runtime. I could add additional ViewModels to the ObservableCollection, but I have no idea how to switch between them. Any help would be appreciated.


Here are some code snippets:



App.xaml.cs
C#
namespace HDI_WPF_MVVMIntro_cs
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            MainWindow window = new MainWindow();
            var viewModel = new MainWindowsViewModel();
            window.DataContext = viewModel;
            window.Show();
        }
    }
}



MainWindowViewModel.cs
C#
using System.Collections.ObjectModel;
using HDI_WPF_MVVMIntro_cs.DataAccess;

namespace HDI_WPF_MVVMIntro_cs.ViewModel
{
    public class MainWindowsViewModel : ViewModelBase
    {
        readonly EmployeeRespository _employeeRepository;

        ObservableCollection<ViewModelBase> _viewModels;

        public MainWindowsViewModel()
        { 
            _employeeRepository = new EmployeeRespository();
            EmployeeListViewModel viewModel = new EmployeeListViewModel  (_employeeRepository);
            this.ViewModels.Add(viewModel);
        }

        public ObservableCollection<ViewModelBase> ViewModels
        {
            get
            {
                if (_viewModels == null)
                {
                    _viewModels = new ObservableCollection<ViewModelBase>();
                }

                return _viewModels;
            }
        }
    }
}


MainWindow.xaml
XML
<Window x:Class="HDI_WPF_MVVMIntro_cs.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:HDI_WPF_MVVMIntro_cs.ViewModel"
        xmlns:vw="clr-namespace:HDI_WPF_MVVMIntro_cs.View"
        Title="MainWindow" Height="350" Width="525">
    
    <Window.Resources>
        <DataTemplate DataType="{x:Type vm:EmployeeListViewModel}">
            <vw:EmployeeListView />
        </DataTemplate>
    </Window.Resources>
    
    <Grid Margin="4">
        <ItemsControl ItemsSource="{Binding ViewModels}" Margin="4" />
    </Grid>
</Window>
Posted
Updated 24-Mar-15 11:01am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900