title: Can I use XamlReader.Load or InitializeFromXaml from a WPF Window, for the Window definition?
body: <p>I want to produce some library code that will be included into WPF applications. The library may pop up a Window, depending on circumstances.  I can define the Window in XAML, but I'd like to treat the XAML as a template.  At runtime, at the time the Window is being created so that it can be displayed, I want to replace certain tags in the Xaml template with runtime-defined values. </p>
  
  <p>What I want to do is something like this: </p>
  
  <pre><code>public partial class DynamicXamlWindow : Window
  {
      Button btnUpdate = null;
      public DynamicXamlWindow()
      {
          string s = XamlTemplate;
  
          // replace some things in the XamlTemplate here
  
          Window root = System.Windows.Markup.XamlReader.Load(...);
          Object _root = this.InitializeFromXaml(new StringReader(s).ReadToEnd()); //??
  
          btnUpdate = // ???
  
          //InitializeComponent();
      }
  </code></pre>
  
  <p>The XamlTemplate string looks like this:</p>
  
  <pre><code>    private string XamlTemplate = @"
      &lt;Window x:Class='@@CLASS'
              xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
              xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
              Title='@@TITLE' 
              Height='346' Width='380'&gt;
  
          &lt;Grid&gt;
            ...
  </code></pre>
  
  <p>I've seen examples where a button or a section is defined in XAML and loaded dynamically.  But this is not a button or section.  The XamlTemplate provides the XAML for the actual Window.</p>
  
  <p>Is this possible either with InitializeFromXaml or <a href="http://msdn.microsoft.com/en-us/library/system.windows.markup.xamlreader.load.aspx" rel="nofollow noreferrer">XamlReader.Load</a> ?  If so, how?</p>
  
  <p>Can I then retrieve the controls defined in the XAML, for example btnUpdate in the code fragment above.  How? </p>