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 289481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:54:39+00:00 2026-05-12T05:54:39+00:00

I have a WPF UserControl that I’d like to inject dependencies into. What’s the

  • 0

I have a WPF UserControl that I’d like to inject dependencies into. What’s the best way to do this with Ninject?

To take a concrete example: I have a UserControl called MapView, and I want to inject an instance of my IDialogueService into it, via either constructor or property injection. Currently, I’m not using any dependency injection, and my control is created in XAML:

<Window x:Class="GameWindow" ...>
    <Grid Name="root">
        <MapView x:Name="mapView" ... />
        <!-- other stuff here -->
    </Grid>
</Window>

Creating an IKernel, and binding the IDialogueService, is straightforward enough. But I’m stuck on how to use the kernel to inject dependencies into my MapView. I’m still a total newbie at Ninject, so maybe there’s something obvious that I’m missing.

I can think of several ways to solve this:

  1. Instantiate the MapView in code. Remove the <MapView ... /> from the XAML, and instead add this to GameWindow’s constructor:

    public GameWindow(IKernel kernel) {
        root.Children.Add(kernel.Get<MapView>());
    }
    

    Downsides: extra complexity from not using XAML; GameWindow has a dependency on IKernel.

  2. Keep the instantiation in XAML, and use property injection instead:

    public GameWindow(IKernel kernel) {
        kernel.Inject(mapView);
    }
    

    Downside: Ninject docs say that Inject() “should not be used for most cases”, so I don’t even know if it does what I think it does, or whether it makes sense to use it here. And GameWindow still has a dependency on IKernel.

  3. Add injectable parameters/properties to GameWindow that pass the values along to the MapView (probably via properties on MapView), and then use Get<GameWindow>(). Downside: now I’m manually passing dependencies all over the place, which is what a DI framework is supposed to automate for me.

  4. After instantiating the GameWindow, walk the logical tree and call IKernel.Inject() on everything. Downsides: again, I don’t know whether Inject() does what I think it does, or whether it’s appropriate here. Caller must remember to do the walk-the-visual-tree-and-inject thing after instantiating a GameWindow.

Is there a better way to do this? Maybe a WPF extension for Ninject that lets me do a Get<GameWindow>(), and automatically walks the logical tree for me (as in #4), doing property injection on everything? If such a thing doesn’t exist, would it be possible for me to write it?

How do you guys approach using Ninject with WPF? Do you use any of the above approaches (and if so, can you share up/downsides I don’t know about)? Do you have better approaches?

  • 1 1 Answer
  • 1 View
  • 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-12T05:54:39+00:00Added an answer on May 12, 2026 at 5:54 am

    1) is best. You’ll also find it’s a convenient entry point for Mapview functions.

    Also, rather than passing in the kernel, just do kernel.Get< GameWindow>() and [inject] MapView in GameWindow.

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

Sidebar

Related Questions

I have a WPF UserControl with binding converters referenced like this: <UserControl x:Class=MyControl x:Name=MyControl
I have got some custom WPF UserControl . And it has implementation like this
Background: I have a WPF UserControl (MainControl - not shown in code below) that
WPF controls have certain properties (UserControl.Resources, UserControl.CommandBindings) that can have items added to them
I have created a UserControl in WPF. This user control consists of a border,
is there a way to have a WPF UserControl Class to be a class
I have a WPF project with an AssemblyInfo.cs that groups multiple CLR namespaces into
I have a WPF UserControl that contains a ComboBox . I need to attach
I have a WPF UserControl that shows up fine in the designer. However, the
I have an WPF usercontrol that has a TextBox. I set the text Underline

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.