title: Creating a custom UI element in WPF
body: <p>I have a several <code>TextBoxes</code> with <code>Labels</code> in my code which are implemented with the following XAML:</p>
  
  <pre><code>&lt;DockPanel HorizontalAlignment="Right"&gt;
      &lt;TextBlock Foreground="Black" Padding="0,0,10,0"&gt;Serial Number:&lt;/TextBlock&gt;
      &lt;TextBox Width="150" IsReadOnly="True" BorderBrush="Gainsboro" Height="20"&gt;&lt;/TextBox&gt;
  &lt;/DockPanel&gt;
  </code></pre>
  
  <p>I can reduce some of the copied code by doing something like:</p>
  
  <pre><code>&lt;DockPanel HorizontalAlignment="Right"&gt;
      &lt;TextBlock Style="{StaticResource CstmTextBoxLbl}"&gt;Serial Number:&lt;/TextBlock&gt;
      &lt;TextBox Style="{StaticResource CstmTextBox}"&gt;&lt;/TextBox&gt;
  &lt;/DockPanel&gt;
  </code></pre>
  
  <p>but it is still somewhat lengthy. Is it possible to do something like:</p>
  
  <pre><code>&lt;controls:CstmTextBox Style="{StaticResource CstmTextBox}" LabelText="Serial Number:" Text=""/&gt;
  </code></pre>
  
  <p>Where the <code>CstmTextBox</code> would implement the whatever XAML is needed to get the same visual effect once, and I could access both the <code>TextBlock</code> text and the <code>TextBox</code> text in the code. Such as</p>
  
  <pre><code>CstmTextBox textbox;
  textbox.LabelText = "Serial Number:";
  String some_text = textbox.Text;
  
  textbox.Text = "....";
  </code></pre>