title: How to prevent InitializeComponent code from constructing a member field / property
body: <p>Firstly I'm very new to WPF (coming from WinForms) and I'm facing quite a difficult problem. Sorry if it's rather lengthy, I've tried to give it structure and shorten it down.</p>
  
  <p><strong>Environment (brief)</strong></p>
  
  <p>I'm using a custom user control <code>CaptureControl</code> inside my <code>MainWindow</code>. This control has an encapsuled field / property of an <code>InputManager</code> object. This <code>InputManager</code> object is instantiated a single time in the global <code>App</code> class. It is later assigned to user control members (e.g. the <code>CaptureControl</code>).</p>
  
  <p><strong>Problem</strong></p>
  
  <p>My problem with this is that for some reason the <code>InitializeComponent()</code> call inside my <code>CaptureControl</code> constructs an instance of <code>InputManager</code> somewhere in <em>external code</em>. The <code>InputManager</code> registers event handlers to a static class during construction and <strong>needs to be disposed</strong> in order to unregister these handlers again. Sadly <strong>whatever constructs an instance of the <code>InputManager</code> class does not dispose it properly again</strong> or keeps it referenced at some place where I can't access it. The <code>_InputManager</code> field remains being null.</p>
  
  <p>This is the problematic code:</p>
  
  <pre><code>public partial class CaptureControl : UserControl
  {
      /// &lt;summary&gt;
      /// &lt;para&gt;Gets or sets the Input Manager to be used&lt;/para&gt;
      /// &lt;/summary&gt;
      public InputManager InputManager
      {
          get =&gt; _InputManager;
          set
          {
              _InputManager = value;
              lstTriggeredKeys.DataContext = _InputManager;
          }
      }
      private InputManager _InputManager;
  
      public CaptureControl()
      {
          InitializeComponent(); // This calls external code which constructs an instance of InputManager
      }
  }
  </code></pre>
  
  <p>This invokes the constructor of my <code>InputManager</code> class:</p>
  
  <pre><code>public class InputManager : IDisposable
  {
      /// &lt;summary&gt;
      /// &lt;para&gt;Constructs the input helper&lt;/para&gt;
      /// &lt;para&gt;Registers static keyboard events&lt;/para&gt;
      /// &lt;/summary&gt;
      public InputManager()
      {
          Interceptor.OnKeyInput += Interceptor_OnKeyInput;
      }
  
      /// &lt;summary&gt;
      /// &lt;para&gt;Releases static keyboard events&lt;/para&gt;
      /// &lt;/summary&gt;
      public void Dispose()
      {
          Interceptor.OnKeyInput -= Interceptor_OnKeyInput;
      }
  
      private void Interceptor_OnKeyInput(KeyInput input)
      { ... }
  }
  </code></pre>
  
  <p>Which results in two problems:</p>
  
  <ol>
  <li>There is an event handler unnecessarily being invoked on an anonymous object</li>
  <li>The dependent clean-up logic on the static class is not fired if the event handler count is higher than 0</li>
  </ol>
  
  <p><strong>Question</strong></p>
  
  <p>I have difficulties understanding why the <code>InitializeComponent()</code> call would construct an instance of <code>InputManager</code> in the first place.</p>
  
  <blockquote>
    <p>How can this be prevented? How can I prevent the external code behind
    <code>InitializeComponent()</code> to construct an instance of
    <code>InputManager</code> for whatever reason?</p>
  </blockquote>
  
  <p><strong>Call Stack</strong></p>
  
  <p>The <strong>external code</strong> call stack behind <code>InitializeComponent()</code> is huge and ends with an invisible native to managed transition. The last visible record before reaching the <code>InputManager</code> constructor is:</p>
  
  <pre><code>&gt; InputData.dll!InputData.InputManager.InputManager() Line 40   C#
  &gt; [Native to Managed Transition]
  &gt; [Managed to Native Transition]
  &gt; mscorlib.dll!System.RuntimeType.CreateInstanceSlow(bool publicOnly, bool skipCheckThis, bool fillCache = true, ref System.Threading.StackCrawlMark stackMark)
  &gt; ...
  &gt; InputH.exe!InputH.CaptureControl.InitializeComponent() Line 1 C#
  &gt; ...
  </code></pre>
  
  <p><strong>Environment (detailed)</strong></p>
  
  <p>The <code>App</code> class which inherits the <code>System.Windows.Application</code> class and constructs the <code>InputManager</code> object. The object is assigned to one or more user controls later on:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Interaction logic for App.xaml
  /// &lt;/summary&gt;
  public partial class App : Application
  {
      // Single instance / global / shared object
      public InputManager InputManager { get; private set; } = new InputManager();
  
      // Disposing of the object
      protected override void OnExit(ExitEventArgs e)
      {
          base.OnExit(e);
          InputManager.Dispose();
      }
  }
  </code></pre>
  
  <p>The global <code>InputManger</code> instance is assigned to the user control in the Constructor of my <code>MainWindow</code>. It also assigns the shared single instance <code>InputManager</code> object to it:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Interaction logic for MainWindow.xaml
  /// &lt;/summary&gt;
  public partial class MainWindow : Window
  {
      public MainWindow()
      {
          InitializeComponent();
  
          captureControl.InputManager = ((App)Application.Current).InputManager;
      }
  }
  </code></pre>