title: How to use Selected Item in a Custom Collection
body: <p>I have a program where I change the collection of items displayed in a <code>comboBox</code>. I've talked about it before in <a href="https://stackoverflow.com/questions/19189623/how-to-change-the-itemssource-of-a-combobox-in-c-sharp">this question</a>. Anyway, I am using a custom collection right now because <code>observableCollection</code> doesn't have a <code>selectedItem</code> property. The custom collection has a <code>selectedItem</code> property in it, but I am just unsure how to set it so that the data is saved.</p>
  
  <p><strong>Custom Collection Class</strong></p>
  
  <pre><code>public class MyCustomCollection&lt;T&gt;: ObservableCollection&lt;T&gt;
  {
      private T _mySelectedItem;
  
      public MyCustomCollection(IEnumerable&lt;T&gt; collection) : base(collection) { }
  
      public T MySelectedItem
      {
          get { return _mySelectedItem; }
          set
          {
              if (Equals(value, _mySelectedItem)) return;
              _mySelectedItem = value;
              OnPropertyChanged(new PropertyChangedEventArgs("MySelectedItem"));
          }
      }
  }
  </code></pre>
  
  <p><strong>ViewModel - Where I change the collection in the <code>comboBox</code> and set the <code>selectedItem</code> for each collection</strong></p>
  
  <pre><code>//Property for the selected command in the LIST BOX
  //**For clarity: the collection in the comboBox is changed based on what is
  //selected in the list box
  public string SelectedCommand
  {
      get { return _selectedCommand; }
      set
      {
          _selectedCommand = value;
          NotifyPropertyChange(() =&gt; SelectedCommand);
  
          if (SelectedCommand == "1st Collection of type A")
          {
              ComboBoxEnabled = true;
              ComboBoxList = new MyCustomCollection&lt;string&gt;(collectionA);
              //collectionA.MySelectedItem = ??(What would I put here?) 
          }
          if (SelectedCommand == "2nd Collection of type A")
          {
              ComboBoxEnabled = true;
              ComboBoxList = new MyCustomCollection&lt;string&gt;(collectionA);
              //collectionA.MySelectedItem = ??(What would I put here?)
          }
      }
  }
  </code></pre>
  
  <p>How would I assign a value to <code>MySelectedItem</code> for each new collection that I create and add to the <code>comboBox</code>? This will make it so that whenever I switch to a different collection in the <code>comboBox</code>, the <code>selectedItem</code> will be displayed.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>My collections are now set as <code>ObservableCollection&lt;string&gt;</code>.</p>
  
  <p>XAML for <code>ListBox</code> and <code>ComboBox</code></p>
  
  <pre><code>&lt;ListBox ItemsSource="{Binding Model.CommandList}" SelectedItem="{Binding Model.SelectedCommand}" ... /&gt;
  
  &lt;ComboBox ItemsSource="{Binding Model.ComboBoxList}" SelectedItem="{Binding Model.SelectedOperation}"  ... /&gt;
  </code></pre>
  
  <p>**New <code>SelectedCommand</code> property under <code>ListBox</code>:</p>
  
  <pre><code>public string SelectedCommand
  {
      get { return _selectedCommand; }
      set
      {
          _selectedCommand = value;
          NotifyPropertyChange(() =&gt; SelectedCommand);
  
          switch (SelectedCommand)
          {
              case "Collection A":
                  {
                      ComboBoxList = CollectionA;
                      break;
                  }
              case "Collection B":
                  {
                      ComboBoxList = CollectionB;
                      break;
                  }
          }
          NotifyPropertyChange(() =&gt; ComboBoxList);
      }
  }
  </code></pre>
  
  <p><strong>The program still does not keep the <code>selectedItem</code> that is selected for each collection.</strong> I must be forgetting, or not understanding something.</p>