title: How to apply TemplateBinding to "Auto" height and width
body: <p>In resource dictionary:</p>
  
  <pre><code>&lt;Style x:Key="ControlFrame" TargetType="{x:Type TextBox}"&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type TextBox}"&gt;
                  &lt;Border Background="{TemplateBinding Background}" BorderThickness="2"&gt;
                      &lt;ScrollViewer Margin="0" x:Name="PART_ContentHost" /&gt;
                      &lt;Border.BorderBrush&gt;
                          &lt;VisualBrush&gt;
                              &lt;VisualBrush.Visual&gt;
                                  &lt;Rectangle StrokeDashArray="8, 2" Stroke="{TemplateBinding BorderBrush}" 
                                             StrokeThickness="2"
                                             Width="{TemplateBinding Width}"
                                             Height="{TemplateBinding Height}"/&gt;
                              &lt;/VisualBrush.Visual&gt;
                          &lt;/VisualBrush&gt;
                      &lt;/Border.BorderBrush&gt;
                  &lt;/Border&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>In C#:</p>
  
  <pre><code>TextBox textbox = new TextBox();
  textbox.Width = 200;
  textbox.Height = 200;
  Style style = this.FindResource("ControlFrame") as Style;
  textbox.Style = style;
  canvas.Children.Insert(0, textbox);
  </code></pre>
  
  <p>I can get the dashed border properly.</p>
  
  <p><img src="https://i.stack.imgur.com/FdXV8.jpg" alt="enter image description here"></p>
  
  <p>If I wrap the <strong>Textbox</strong> into a <strong>ContentControl</strong> without giving the height and width of the <strong>Textbox</strong> like the following:</p>
  
  <pre><code>TextBox textbox = new TextBox();
  Style style = this.FindResource("ControlFrame") as Style;
  textbox.Style = style;
  ContentControl cc = new ContentControl();
  cc.Content = textbox;
  cc.Height = 200;
  cc.Width = 200;
  canvas.Children.Insert(0, cc);
  </code></pre>
  
  <p>The result is missed up:</p>
  
  <p><img src="https://i.stack.imgur.com/mb2uY.jpg" alt="enter image description here"></p>
  
  <p>I guess the reason is:</p>
  
  <p>In Style, I use the following to set Width and Height of the border. They rely on the <strong>TextBox</strong>'s Width and Height. </p>
  
  <pre><code>Width="{TemplateBinding Width}"
  Height="{TemplateBinding Height}"
  </code></pre>
  
  <p>If I wrap the <strong>TextBox</strong> into a <strong>ContentControl</strong>, <strong>TextBox</strong>'s Width and Height are set to Auto and changed according to the <strong>ContentControl</strong>. But, the <strong>Style</strong> cannot get the exact Height and Width any more.</p>
  
  <p>My question is:</p>
  
  <p>Is there any way to let my <strong>Style</strong> display properly for a <strong>TextBox</strong> wrapped within a <strong>ContentControl</strong>. Since the <strong>ContentControl</strong> is draggable, I cannot set exact Height and Width to the inside <strong>TextBox</strong>.</p>