title: Labeled ComboBox in Windows 10 Universal App
body: <p>similar to my Labeled TextBox, which issues are resolved in:</p>
  
  <p><a href="https://stackoverflow.com/questions/32512469/labeled-textbox-in-windows-universal-app">Labeled TextBox in Windows Universal App</a></p>
  
  <p>I got two issues in my Labeled Combobox, but first the Code:</p>
  
  <p><strong>Generic.xaml</strong>:</p>
  
  <pre><code>&lt;Style TargetType="template:LabeledComboBox"&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="template:LabeledComboBox"&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;ComboBox x:Name="PART_ComboBox" ItemsSource="{TemplateBinding ItemsSource}" SelectedIndex="{TemplateBinding SelectedIndex}" SelectedValue="{TemplateBinding SelectedValue}" SelectedValuePath="{TemplateBinding SelectedValuePath}" DisplayMemberPath="{TemplateBinding DisplayMemberPath}" 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><strong>LabeledComboBox.cs</strong>:</p>
  
  <pre><code>[TemplatePart(Name = "PART_ComboBox", Type = typeof(ComboBox))]
  public sealed class LabeledComboBox : Control, IParameterReturnable
  {
      public static readonly DependencyProperty LabelProperty = DependencyProperty.Register("Label", typeof(string), typeof(LabeledComboBox), new PropertyMetadata(""));
      public string Label
      {
          get { return GetValue(LabelProperty).ToString(); }
          set { SetValue(LabelProperty, value); }
      }
  
      public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(object), typeof(LabeledComboBox), new PropertyMetadata(null));
      public object ItemsSource
      {
          get { return GetValue(ItemsSourceProperty); }
          set { SetValue(ItemsSourceProperty, value); }
      }
  
      public static readonly DependencyProperty SelectedIndexProperty = DependencyProperty.Register("SelectedIndex", typeof(int), typeof(LabeledComboBox), new PropertyMetadata(default(int)));
      public int SelectedIndex
      {
          get { return (int) GetValue(SelectedIndexProperty); }
          set { SetValue(SelectedIndexProperty, value); }
      }
  
      public static readonly DependencyProperty SelectedValueProperty = DependencyProperty.Register("SelectedValue", typeof(object), typeof(LabeledComboBox), new PropertyMetadata(null));
      public object SelectedValue
      {
          get { return GetValue(SelectedValueProperty); }
          set { SetValue(SelectedValueProperty, value); }
      }
  
      public static readonly DependencyProperty SelectedValuePathProperty = DependencyProperty.Register("SelectedValuePath", typeof(string), typeof(LabeledComboBox), new PropertyMetadata(default(string)));
      public string SelectedValuePath
      {
          get { return GetValue(SelectedValuePathProperty).ToString(); }
          set { SetValue(SelectedValuePathProperty, value); }
      }
  
      public static readonly DependencyProperty DisplayMemberPathProperty = DependencyProperty.Register("DisplayMemberPath", typeof(string), typeof(LabeledComboBox), new PropertyMetadata(default(string)));
      public string DisplayMemberPath
      {
          get { return GetValue(DisplayMemberPathProperty).ToString(); }
          set { SetValue(DisplayMemberPathProperty, value); }
      }
  
      private ComboBox _comboBox;
  
      public LabeledComboBox()
      {
          this.DefaultStyleKey = typeof(LabeledComboBox);
      }
  
      public LabeledComboBox(List&lt;Parameter&gt; parameterList)
      {
          this.Label = parameterList[0].DisplayName ?? "";
          this.ItemsSource = parameterList;
          this.SelectedValuePath = "DefaultValue";
          this.DisplayMemberPath = "DefaultValue";
          this.SelectedIndex = 0;
          this.DefaultStyleKey = typeof(LabeledComboBox);
      }
  
      protected override void OnApplyTemplate()
      {
          base.OnApplyTemplate();
  
          _comboBox = GetTemplateChild("PART_ComboBox") as ComboBox;
  
          if (_comboBox != null)
          {
              _comboBox.SelectionChanged += OnComboBoxSelectionChanged;
              if (_comboBox.Items != null)
              {
                  this.SelectedIndex = 0;
                  _comboBox.SelectedValue = _comboBox.Items[this.SelectedIndex];
              }
          }
      }
  
      private void OnComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
      {
          this.SelectedValue = _comboBox.SelectedValue;
      }
  
      public string GetKey()
      {
          return Label;
      }
  
      public string GetValue()
      {
          return SelectedValue.ToString();
      }
  
  }
  </code></pre>
  
  <p>It will be called in two different ways:</p>
  
  <p><strong>Dynamically in C#</strong>:</p>
  
  <pre><code>stackPanel.Add(new LabeledComboBox(parameterList));
  </code></pre>
  
  <p><strong>Static in Xaml</strong>:</p>
  
  <pre><code>&lt;templates:LabeledComboBox Label="Kategorien:" ItemsSource="{Binding ElementName=pageRoot, Path=FeedCategories}" DisplayMemberPath="Name" SelectedValuePath="Name" /&gt;
  </code></pre>
  
  <p>As I said before I got two issues with it:</p>
  
  <ol><li>How can I bind the SelectionChangedEvent to access it in Xaml || C#</li>
  <li>As you can see, I try to preselect the first Item, which does not work and I don't know how to do it right</li>
  </ol>
  
  <p>Thank you very much for all helpful and well meant answers in advance!</p>