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

  • Home
  • SEARCH
  • 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 7049835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:04:11+00:00 2026-05-28T03:04:11+00:00

I have been learning MVVM / WPF and have gone through the tutorial here

  • 0

I have been learning MVVM / WPF and have gone through the tutorial here.

I have created a working application using this methodology but now, on a new project, I cannot get the Dependency Injection to work.

When I run this project I get an empty MainWindow without the CompanyView injected. I have double and tripled checked everything between the project that works and this one that doesn’t and cannot find the reason for CompanyView not being injected. I have also tried cleaning the solution and restarting VS to no avail. Hopefully someone can see what I am missing.

I have the following file:

App.xaml.cs (using base.OnStartup() instead of StartupUri in App.xaml)

namespace SidekickAdmin
{
    /// <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 MainWindowViewModel();
            window.DataContext = viewModel;
            window.Show();
        }

    }
}

MainWindowViewModel.cs

namespace SidekickAdmin.ViewModel
{
    class MainWindowViewModel : ViewModelBase
    {
        public MainWindowViewModel()
        {
            CompanyViewModel companyViewModel = new CompanyViewModel(_repository);
            this.ViewModels.Add(companyViewModel);

        }

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

MainWindowView.xaml

<Window x:Class="SidekickAdmin.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:SidekickAdmin.ViewModel"
        xmlns:vw="clr-namespace:SidekickAdmin.View"
        Title="Sidekick Admin" SizeToContent="WidthAndHeight">

    <!-- Typically done in a resources dictionary -->
    <Window.Resources>
        <ResourceDictionary Source="MainWindowResources.xaml" />
    </Window.Resources>


    <StackPanel>
        <ItemsControl ItemsSource="{Binding ViewModel}" Margin="3" /> 
    </StackPanel>

</Window>

MainWindowResources.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:vm="clr-namespace:SidekickAdmin.ViewModel"
                    xmlns:vw="clr-namespace:SidekickAdmin.View">

    <DataTemplate DataType="{x:Type vm:CompanyViewModel}">
        <vw:CompanyView />
    </DataTemplate>

</ResourceDictionary>

CompanyViewModel.cs (not really used yet as I am still trying to just get the view to appear)

namespace SidekickAdmin.ViewModel
{

    class CompanyViewModel : ViewModelBase
    {
        readonly GenericRepository _repository;

        #region Getters & Setters
        public ObservableCollection<Company> AllCompanies
        {
            get;
            private set;
        }

        #endregion

        #region Constructors
        public CompanyViewModel(GenericRepository repository)
        {

            if (repository == null)
            {
                throw new ArgumentNullException("repository");
            }

            _repository = repository;

            this.AllCompanies = new ObservableCollection<Company>(_repository.GetAll<Company>());
        }

        #endregion


        protected override void OnDispose()
        {
            this.AllCompanies.Clear();
        }

    }
}

CompanyView.xaml

<UserControl x:Class="SidekickAdmin.View.CompanyView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             Height="300" Width="300">

    <StackPanel>
        <TextBlock>You say Hello</TextBlock>
        <TextBlock>And I say Goodbye</TextBlock>    
        <TextBlock>Hello, Hello</TextBlock>
    </StackPanel>

 </UserControl>
  • 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-28T03:04:11+00:00Added an answer on May 28, 2026 at 3:04 am

    Besides what @Robert Levy has wrote, the error you are making is that your ViewModels property is private, make it public and it should work fine.

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

Sidebar

Related Questions

I am currently learning MVVM abd the tutorial I have been using, simply uses
I have been learning RESTful webservices following this tutorial http://www.vogella.de/articles/REST/article.html . As I understand,
I have been learning C++ for three months now and in that time created
I have been learning python for some time now. While starting this learning python
I have been learning about TDD (using JUnit) and I have a doubt about
I have been learning MVVM/EF4 (for C#) recently and have followed Julie Lerman's videos.
I have been learning some of the more detailed loopholes of JS here and
I have very recently been learning python. I have been using pyscripter as is
I have been using the DelegateCommand found in the MVVM Visual Studio template at
I have been learning how to create dynamic images and dynamic stylesheets using ASP.NET

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.