title: How to get or set custom object as datacontext instance to custom control in wpf
body: <p>I've created a custom control that is styled and configures in its own XAML sheet. Databindings in this control uses a specific object (CProject class).</p>
  
  <p>Just to clarify, the control is a project frame, that has controls for settings and a canvas that will be the workspace for each/any project.</p>
  
  <p>The project control (IPProjectPanel) inherits "Frame", and also adds a "settings" stack panel to its children list which in turn contains controls for - well, settings.</p>
  
  <p>The CProject class however, is the pure functional part, with no UI interaction or handling whatsoever. So, I need to "plug" an instance of CProject into every unique project that can be active. So, I want to set a specific instance of CProject as datacontext to every IPProjectPanel instance in a tabpanel. Either I want to set the datacontext by code, or have it created by settings datacontext in XAML, and retrieving it after it has been initialized.</p>
  
  <p>The problem though, is that I cant quite figure out either.</p>
  
  <p>Here is a snippet of the style of the IPProjectPanel in XAML, that uses the approach to set datacontext in XAML:</p>
  
  <pre><code>  &lt;Style TargetType="{x:Type ip:IPProjectGrid}"&gt;
      &lt;Setter Property="OverridesDefaultStyle"
          Value="True" /&gt;
      &lt;Setter Property="SnapsToDevicePixels"
          Value="True" /&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type ip:IPProjectGrid}"&gt;
                  &lt;Grid Background="White"  
                        HorizontalAlignment="Stretch" 
                        VerticalAlignment="Stretch" 
                        Margin="0"&gt;
                      &lt;!----&gt;
                      &lt;Grid.DataContext&gt;
                          &lt;ipp:CProject&gt;&lt;/ipp:CProject&gt;
                      &lt;/Grid.DataContext&gt;
  
                      &lt;StackPanel x:Name="PART_settingsPanel" 
                                  HorizontalAlignment="Right" 
                                  VerticalAlignment="Stretch"
                                  MinWidth="300" Background="Gray"&gt;
  
                          &lt;GroupBox Header="Project settings"&gt;
                              &lt;StackPanel&gt;
  
    ....
  
    &lt;/style&gt;
  </code></pre>
  
  <p>Here it is set as a context to Grid, but I'd like to have it as a context of the actual class (IPProjectPanel).</p>
  
  <p>So, the IPProjectPanel instance is created by code (for now..), and I need to retrieve the CProject instance (or set one) so that I can work with it.</p>
  
  <p>I'd like to keep to C#/WPF ways to do stuff, as this app is also training for WPF and C# concepts and such. So the "best C#-WPF" way to do it, is very welcome, but a solution either way!</p>
  
  <p>Thank you for your time.</p>