Skip to content

Instantly share code, notes, and snippets.

@sajidmohammed88
Last active November 5, 2025 07:00
Show Gist options
  • Select an option

  • Save sajidmohammed88/99928bbb3e94028fc6be175d67c0b46f to your computer and use it in GitHub Desktop.

Select an option

Save sajidmohammed88/99928bbb3e94028fc6be175d67c0b46f to your computer and use it in GitHub Desktop.
Dependency injection in WPF project with .net core

Dependency injection in WPF project with .net core

Delete StartupUri="MainWindow.xaml" from App.xaml
Install packages :
Microsoft.Extensions.Configuration.Json
Microsoft.Extensions.DependencyInjection
Microsoft.Extensions.Options.ConfigurationExtensions
Add the file appsettings.json in the root of the project to use it for configuration.
Override OnStartup method in App.Xaml.Cs class :
public IServiceProvider ServiceProvider { get; private set; }

public IConfiguration Configuration { get; private set; }

protected override void OnStartup(StartupEventArgs e)
{
    var builder = new ConfigurationBuilder()
     .SetBasePath(Directory.GetCurrentDirectory())
     .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

    Configuration = builder.Build();

    var serviceCollection = new ServiceCollection();
    ConfigureServices(serviceCollection);

    ServiceProvider = serviceCollection.BuildServiceProvider();

    var mainWindow = ServiceProvider.GetRequiredService<MainWindow>();
    mainWindow.Show();
}

private void ConfigureServices(IServiceCollection services)
{
    services.Configure<AppSettings>(Configuration.GetSection(nameof(AppSettings)));

    services.AddTransient(typeof(MainWindow));
}
@Montago
Copy link

Montago commented Feb 4, 2025

Hi again (sorry for the long delay)

The problem with constructor injection in WPF is that usercontrol hierachy and how one would normally write XAML, wont use the serviceprovider to fetch usercontrols.

eg

<VehicleManager> 
    <VehicleList>
       <ItemsControl>
             <Vehicle>

Vehicles are generated on the fly by the XAML binding and wont be requested from the ServiceProvider.
Usually controls are spawned from the UI rather than from the codebehind...

So i dont know how useful DI is for WPF...

@LoriBru
Copy link

LoriBru commented Nov 4, 2025

I am struggling to pass the viewmodel to the view as a parameter constructor, while still being able to design my UI in xaml.
I randomly came across this discussion, is there any possibility that you found an elegant solution?

@Montago
Copy link

Montago commented Nov 5, 2025

I am struggling to pass the viewmodel to the view as a parameter constructor, while still being able to design my UI in xaml. I randomly came across this discussion, is there any possibility that you found an elegant solution?

If your viewmodel only exists once: use Singleton and simply reference it in your usercontrol

if your viewmodel exists pr usercontrol / window, instantiate a new one and pass it to the constructor or instantiate it in the constructor .... or do everything like MVC and let the codebehind be the Viewmodel ... usually the easiest option.

ViewModels only make sense if they aggregate several UserControls

@Montago
Copy link

Montago commented Nov 5, 2025

btw.. I've given up on using DI for WPF .. it makes no sense

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment