title: Labeled TextBox in Windows Universal App
body: <p>I'm trying to realize a labeled <code>TextBox</code> in a windows universal app using <em>C#</em> and <em>XAML</em>, but I did not get the value of the <code>TextBox</code> in return, just empty string. </p>
  
  <p>The code of my <strong>generic.xaml</strong> for it looks like this:</p>
  
  <pre><code>&lt;Style TargetType="template:LabeledTextBox"&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="template:LabeledTextBox"&gt;
                  &lt;Grid&gt;
                      &lt;Grid.RowDefinitions&gt;
                          &lt;RowDefinition Height="*" /&gt;
                          &lt;RowDefinition Height="*" /&gt;
                      &lt;/Grid.RowDefinitions&gt;
                      &lt;TextBlock Text="{TemplateBinding Label}" FontWeight="Bold" VerticalAlignment="Center" Margin="10,0" /&gt;
                      &lt;TextBox Text="{TemplateBinding Value}" IsReadOnly="{TemplateBinding IsReadOnly}" VerticalAlignment="Center" Margin="20,0,10,0" Grid.Row="1" /&gt;
                  &lt;/Grid&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>The control looks like this:</p>
  
  <pre><code>public sealed class LabeledTextBox : Control, IParameterReturnable
  {
      public static readonly DependencyProperty LabelProperty = DependencyProperty.Register("Label", typeof(string), typeof(LabeledTextBox), new PropertyMetadata(default(string)));
      public string Label
      {
          get { return this.GetValue(LabelProperty).ToString(); }
          set { this.SetValue(LabelProperty, value); }
      }
  
      public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(LabeledTextBox), new PropertyMetadata(default(string)));
      public string Value
      {
          get { return this.GetValue(ValueProperty).ToString(); }
          set { this.SetValue(ValueProperty, value); }
      }
  
      public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(LabeledTextBox), new PropertyMetadata(false));
      public string IsReadOnly
      {
          get { return this.GetValue(IsReadOnlyProperty).ToString(); }
          set { this.SetValue(IsReadOnlyProperty, value); }
      }
  
      public LabeledTextBox()
      {
          this.DefaultStyleKey = typeof(LabeledTextBox);
      }
  
      public LabeledTextBox(Parameter parameter)
      {
          this.Label = parameter.DisplayName;
          this.Value = parameter.DefaultValue ?? "";
          this.DefaultStyleKey = typeof(LabeledTextBox);
      }
  
      public string GetKey()
      {
          return this.Label;
      }
  
      public string GetValue()
      {
          return this.Value;
      }
  }
  </code></pre>
  
  <p>You are free to ignore my <em>IParameterReturnable</em>, which I am using to access to <code>GetKey()</code> and <code>GetValue()</code> while looping through different types of these labeled-controls.</p>
  
  <p>So what am I missing, what am I doing wrong?</p>
  
  <p>Thank you very much for all helpful and well meant answers!</p>
  
  <p><strong>EDIT: Code for adding Control</strong></p>
  
  <p>The control is added dynamically with C# like this:</p>
  
  <pre><code>foreach (Parameter parameter in parameters)
  {
      stackPanel.Children.Add(new LabeledTextBox(parameter));
  }
  </code></pre>
  
  <p>But it would looks like this in Xaml:</p>
  
  <pre><code>&lt;templates:LabeledTextBox Label="[Label]" Value="[Value]" IsReadOnly="False" /&gt;
  </code></pre>
  
  <p>It will be read out like this:</p>
  
  <pre><code>foreach(IParameterReturnable parameter in stackPanel.Children)
  {
      string val = parameter.GetValue() // &lt;= this will always return the default value
  }
  </code></pre>