title: WPF binding with custom control
body: <p>I am currently designing a system, (my first in WPF) and i am looking to bind properties of my custom control to elements set within the control-style.</p>
  
  <pre><code>&lt;Style TargetType="{x:Type c:Connection}"&gt;
      &lt;Setter Property="SnapsToDevicePixels" Value="true"/&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type c:Connection}"&gt;
                  &lt;Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"&gt;
                      &lt;Line Stroke="Red" X1="90" X2="90" Y1="90" Y2="5"/&gt;
                  &lt;/Grid&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>I am looking to bind the elements of the Line (X1/2 Y1/2) to properties within my Connection Control. However, as soon as i add a connection element (in code, even without binding), i get a Type Initialization Error. My Connection class is as follows:</p>
  
  <pre><code>public class Connection : Control
  {
      public Connector StartElement { get; set; }
      public Connector EndElement { get; set; }
  
     #region Properties
  
  
      #endregion
  
  }
  </code></pre>
  
  <p>And I Initialize as follows: Connection con = new Connection(); (and then i add it to the canvas).</p>
  
  <p>How can i bind the coordinates to a the Points that are in Connector? (ex StartElement.GetXPosition());</p>
  
  <p>Kind Regards
  Tom</p>