title: Issue with multiselect combobox control in Windows 8
body: <p>I am creating a multiselect combobox in Windows 8 as shown in below image:</p>
  
  <p><img src="https://i.stack.imgur.com/RJt7Z.jpg" alt="enter image description here"></p>
  
  <p>For this I have created custom control code for which is mentioned below:
  The problem with below code is that </p>
  
  <ol>
  <li>on selecting all the all items are not selected</li>
  <li>Selected items are not displayed in textbox</li>
  </ol>
  
  <p>How can I fix that?</p>
  
  <p>XAML:</p>
  
  <pre><code>&lt;UserControl
      x:Class="App5.MultiSelectComboBox"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="using:App5"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d"
      d:DesignHeight="300"
      x:Name="thisUC"
      d:DesignWidth="400"&gt;
  
          &lt;ComboBox
          x:Name="MultiSelectCombo"  
          ScrollViewer.HorizontalScrollBarVisibility="Auto"
          ScrollViewer.VerticalScrollBarVisibility="Auto"
  Width="400" Height="20" 
                             &gt;
          &lt;ComboBox.ItemTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;CheckBox Content="{Binding Title}" Foreground="Black"
                            IsChecked="{Binding ElementName=thisUC, Path=IsSelected, Mode=TwoWay}"
                             Click="CheckBox_Click" /&gt;
              &lt;/DataTemplate&gt;
          &lt;/ComboBox.ItemTemplate&gt;
          &lt;ControlTemplate TargetType="ComboBox"&gt;
              &lt;Grid &gt;
                  &lt;ToggleButton 
                          x:Name="ToggleButton" 
                         Grid.Column="2" IsChecked="{TemplateBinding IsDropDownOpen}"
  
                          ClickMode="Press" HorizontalContentAlignment="Left" &gt;
                      &lt;ToggleButton.Template&gt;
                          &lt;ControlTemplate&gt;
                              &lt;Grid&gt;
                                  &lt;Grid.ColumnDefinitions&gt;
                                      &lt;ColumnDefinition Width="*"/&gt;
                                      &lt;ColumnDefinition Width="18"/&gt;
                                  &lt;/Grid.ColumnDefinitions&gt;
                                  &lt;Border
                    x:Name="Border" 
                    Grid.ColumnSpan="2"
                    CornerRadius="2"
                    Background="White"
                    BorderBrush="Black"
                    BorderThickness="1,1,1,1" /&gt;
                                  &lt;Border 
                      x:Name="BorderComp" 
                    Grid.Column="0"
                    CornerRadius="2" 
                    Margin="1" 
                   Background="White"
                    BorderBrush="Black"
                    BorderThickness="0,0,0,0" &gt;
                                      &lt;TextBlock Foreground="Black" Text="{TemplateBinding TextValue}" 
                              Padding="3" /&gt;
                                  &lt;/Border&gt;
                                  &lt;Path 
                    x:Name="Arrow"
                    Grid.Column="1"     
                    Fill="Black"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    Data="M 0 0 L 4 4 L 8 0 Z"/&gt;
                              &lt;/Grid&gt;
                          &lt;/ControlTemplate&gt;
                      &lt;/ToggleButton.Template&gt;
                  &lt;/ToggleButton&gt;
                  &lt;Popup 
                          Name="Popup"
                           IsOpen="{TemplateBinding IsDropDownOpen}"
                          &gt;
                      &lt;Grid 
                          Name="DropDown"
                          MinWidth="{TemplateBinding ActualWidth}"
                          MaxHeight="{TemplateBinding MaxDropDownHeight}"&gt;
                          &lt;Border x:Name="DropDownBorder"
                                     BorderThickness="1" Background="White"
                                      BorderBrush="Black"/&gt;
                          &lt;ScrollViewer Margin="4,6,4,6" DataContext="{Binding}"&gt;
                              &lt;StackPanel  /&gt;
                          &lt;/ScrollViewer&gt;
                      &lt;/Grid&gt;
                  &lt;/Popup&gt;
              &lt;/Grid&gt;
              &lt;!--&lt;ControlTemplate.Triggers&gt;
                      &lt;Trigger Property="HasItems" Value="false"&gt;
                          &lt;Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/&gt;
                      &lt;/Trigger&gt;
                      &lt;Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true"&gt;
                          &lt;Setter TargetName="DropDownBorder" Property="CornerRadius" Value="4"/&gt;
                          &lt;Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0"/&gt;
                      &lt;/Trigger&gt;
                  &lt;/ControlTemplate.Triggers&gt;--&gt;
          &lt;/ControlTemplate&gt;
  
      &lt;/ComboBox&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>C#</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Collections.ObjectModel;
  using System.ComponentModel;
  using System.IO;
  using System.Linq;
  using System.Text;
  using Windows.Foundation;
  using Windows.Foundation.Collections;
  using Windows.UI.Xaml;
  using Windows.UI.Xaml.Controls;
  using Windows.UI.Xaml.Controls.Primitives;
  using Windows.UI.Xaml.Data;
  using Windows.UI.Xaml.Input;
  using Windows.UI.Xaml.Media;
  using Windows.UI.Xaml.Navigation;
  
  // The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236
  
  namespace App5
  {
      public sealed partial class MultiSelectComboBox : UserControl
      {
          private ObservableCollection&lt;Node&gt; _nodeList;
          public MultiSelectComboBox()
          {
              InitializeComponent();
              _nodeList = new ObservableCollection&lt;Node&gt;();
          }
  
          #region Dependency Properties
  
          public static readonly DependencyProperty ItemsSourceProperty =
               DependencyProperty.Register("ItemsSource", typeof(Dictionary&lt;string, object&gt;), typeof(MultiSelectComboBox), new PropertyMetadata(null,
          new PropertyChangedCallback(MultiSelectComboBox.OnItemsSourceChanged)));
  
          public static readonly DependencyProperty SelectedItemsProperty =
           DependencyProperty.Register("SelectedItems", typeof(Dictionary&lt;string, object&gt;), typeof(MultiSelectComboBox), new PropertyMetadata(null,
       new PropertyChangedCallback(MultiSelectComboBox.OnSelectedItemsChanged)));
  
          public static readonly DependencyProperty TextProperty =
             DependencyProperty.Register("TextValue", typeof(string), typeof(MultiSelectComboBox), new PropertyMetadata(null));
  
          public static readonly DependencyProperty DefaultTextProperty =
              DependencyProperty.Register("DefaultText", typeof(string), typeof(MultiSelectComboBox), new PropertyMetadata(null));
  
  
  
          public Dictionary&lt;string, object&gt; ItemsSource
          {
              get { return (Dictionary&lt;string, object&gt;)GetValue(ItemsSourceProperty); }
              set
              {
                  SetValue(ItemsSourceProperty, value);
              }
          }
  
          public Dictionary&lt;string, object&gt; SelectedItems
          {
              get { return (Dictionary&lt;string, object&gt;)GetValue(SelectedItemsProperty); }
              set
              {
                  SetValue(SelectedItemsProperty, value);
              }
          }
  
          public string TextValue
          {
              get { return (string)GetValue(TextProperty); }
              set { SetValue(TextProperty, value); }
          }
  
          public string DefaultText
          {
              get { return (string)GetValue(DefaultTextProperty); }
              set { SetValue(DefaultTextProperty, value); }
          }
          #endregion
  
          #region Events
          private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
          {
              MultiSelectComboBox control = (MultiSelectComboBox)d;
              control.DisplayInControl();
          }
  
          private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
          {
              MultiSelectComboBox control = (MultiSelectComboBox)d;
              control.SelectNodes();
              control.SetText();
          }
  
          private void CheckBox_Click(object sender, RoutedEventArgs e)
          {
              CheckBox clickedBox = (CheckBox)sender;
  
              if (clickedBox.Content == "All")
              {
                  if (clickedBox.IsChecked.Value)
                  {
                      foreach (Node node in _nodeList)
                      {
                          node.IsSelected = true;
                      }
                  }
                  else
                  {
                      foreach (Node node in _nodeList)
                      {
                          node.IsSelected = false;
                      }
                  }
  
              }
              else
              {
                  int _selectedCount = 0;
                  foreach (Node s in _nodeList)
                  {
                      if (s.IsSelected &amp;&amp; s.Title != "All")
                          _selectedCount++;
                  }
                  if (_selectedCount == _nodeList.Count - 1)
                      _nodeList.FirstOrDefault(i =&gt; i.Title == "All").IsSelected = true;
                  else
                      _nodeList.FirstOrDefault(i =&gt; i.Title == "All").IsSelected = false;
              }
              SetSelectedItems();
              SetText();
  
          }
          #endregion
  
  
          #region Methods
          private void SelectNodes()
          {
              foreach (KeyValuePair&lt;string, object&gt; keyValue in SelectedItems)
              {
                  Node node = _nodeList.FirstOrDefault(i =&gt; i.Title == keyValue.Key);
                  if (node != null)
                      node.IsSelected = true;
              }
          }
  
          private void SetSelectedItems()
          {
              if (SelectedItems == null)
                  SelectedItems = new Dictionary&lt;string, object&gt;();
              SelectedItems.Clear();
              foreach (Node node in _nodeList)
              {
                  if (node.IsSelected &amp;&amp; node.Title != "All")
                  {
                      if (this.ItemsSource.Count &gt; 0)
  
                          SelectedItems.Add(node.Title, this.ItemsSource[node.Title]);
                  }
              }
          }
  
          private void DisplayInControl()
          {
              _nodeList.Clear();
              if (this.ItemsSource.Count &gt; 0)
                  _nodeList.Add(new Node("All"));
              foreach (KeyValuePair&lt;string, object&gt; keyValue in this.ItemsSource)
              {
                  Node node = new Node(keyValue.Key);
                  _nodeList.Add(node);
              }
  
              MultiSelectCombo.ItemsSource = _nodeList;
          }
  
          private void SetText()
          {
              if (this.SelectedItems != null)
              {
                  StringBuilder displayText = new StringBuilder();
                  foreach (Node s in _nodeList)
                  {
                      if (s.IsSelected == true &amp;&amp; s.Title == "All")
                      {
                          displayText = new StringBuilder();
                          displayText.Append("All");
                          break;
                      }
                      else if (s.IsSelected == true &amp;&amp; s.Title != "All")
                      {
                          displayText.Append(s.Title);
                          displayText.Append(',');
                      }
                  }
                  this.TextValue = displayText.ToString().TrimEnd(new char[] { ',' });
              }
              // set DefaultText if nothing else selected
              if (string.IsNullOrEmpty(this.TextValue))
              {
                  this.TextValue = this.DefaultText;
              }
          }
  
  
          #endregion
      }
  
      public class Node : INotifyPropertyChanged
      {
  
          private string _title;
          private bool _isSelected;
          #region ctor
          public Node(string title)
          {
              Title = title;
          }
          #endregion
  
          #region Properties
          public string Title
          {
              get
              {
                  return _title;
              }
              set
              {
                  _title = value;
                  NotifyPropertyChanged("Title");
              }
          }
          public bool IsSelected
          {
              get
              {
                  return _isSelected;
              }
              set
              {
                  _isSelected = value;
                  NotifyPropertyChanged("IsSelected");
              }
          }
          #endregion
  
          public event PropertyChangedEventHandler PropertyChanged;
          protected void NotifyPropertyChanged(string propertyName)
          {
              if (PropertyChanged != null)
              {
                  PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
              }
          }
  
      }
  }
  </code></pre>