title: Binding listbox to observablecollection
body: <p>I need some help to correctly bind an ObservableCollection to xaml. I can bind the data correctly but when the data changed, the changes did not reflect on the screen. I have read related blogs and seemed to have understood, but when trying to apply what I know into my own sample, it does not work quite as I have thought.</p>
  
  <p>I have 2 classes Fruits and Fruit, where Fruits is a observablecollection of fruit which implements INotifyPropertyChanged</p>
  
  <pre><code>namespace TestCommand.Models
  {
      public class Fruit:INotifyPropertyChanged
      {
          private string _fruitname;
          public string FruitName
          { get
              {
                  return _fruitname;
              }
              set
              {
                  if (_fruitname!=value)
                  {
                      _fruitname = value;
                      OnPropertyChanged("FruitName");
                  }
              }
          }
  
          private string _fruitcolor;
          public string FruitColor
          {
              get
              {
                  return _fruitcolor;
              }
              set
              {
                  if (_fruitcolor != value)
                  {
                      _fruitcolor = value;
                      OnPropertyChanged("FruitColor");
                  }
              }
          }
  
          private bool _selected;
          public bool bSelected
          {
              get
              {
                  return _selected;
              }
              set
              {
                  if (_selected != value)
                  {
                      _selected = value;
                      OnPropertyChanged("bSelected");
                  }
              }
          }
  
          public event PropertyChangedEventHandler PropertyChanged;
          public void OnPropertyChanged(string name)
          {
              if (PropertyChanged!=null)
              {
                  PropertyChanged(this, new PropertyChangedEventArgs(name));
              }
          }
      }
  }
  
      namespace TestCommand.Models
      {
          public class Fruits
          {
              private static ObservableCollection&lt;Fruit&gt; _fruitList;
              public static void Add(string f, string c)
              {
                  _fruitList.Add(new Fruit
                  {
                      FruitName = f,
                      FruitColor = c,
                      bSelected = false
                  });
              }
              static Fruits()
              {
                  _fruitList = new ObservableCollection&lt;Fruit&gt;();
                  _fruitList.Add(new Fruit
                  {
                      FruitName = "Mango",
                      FruitColor = "Yellow",
                      bSelected = false
                  });
                  _fruitList.Add(new Fruit
                  {
                      FruitName = "Mango",
                      FruitColor = "Yellow",
                      bSelected = false
                  });
                  _fruitList.Add(new Fruit
                  {
                      FruitName = "Water Melon",
                      FruitColor = "Green",
                      bSelected = false
                  });
                  _fruitList.Add(new Fruit
                  {
                      FruitName = "Apple",
                      FruitColor = "Red",
                      bSelected = false
                  });
                  _fruitList.Add(new Fruit
                  {
                      FruitName = "Banana",
                      FruitColor = "Yellow",
                      bSelected = false
                  });
                  _fruitList.Add(new Fruit
                  {
                      FruitName = "Orange",
                      FruitColor = "Orange",
                      bSelected = false
                  });
              }
              public static ObservableCollection&lt;Fruit&gt; getAllFruit(bool bSelected = false)
              {
                  var result = (bSelected ?
                                              _fruitList.Where(x =&gt; x.bSelected = true).ToList&lt;Fruit&gt;()
                                              : _fruitList.ToList&lt;Fruit&gt;());
                  return new ObservableCollection&lt;Fruit&gt;(result);
              }
          }
      }
  </code></pre>
  
  <p>My xaml:</p>
  
  <pre><code>&lt;Window x:Class="TestCommand.MainWindow"
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                  xmlns:local="clr-namespace:TestCommand"
                  xmlns:MyCommands='clr-namespace:TestCommand.Commands'
                  mc:Ignorable="d"
                  Title="MainWindow"
                  Height="350"
                  Width="525"&gt;
      &lt;StackPanel Orientation='Vertical' Margin='10'&gt;
          &lt;ListBox x:Name='MyList' ItemTemplate='{StaticResource FruitTemp}'&gt;
          &lt;/ListBox&gt;
          &lt;Button x:Name='AddFruit'
                          Height='auto'
                          Width='auto'
                          Content='Add New Fruit 2'
                          Margin='0,10,0,0'
                          Command='{x:Static MyCommands:TestButtonCommand.AddFruit}'&gt;
              &lt;Button.CommandBindings&gt;
                  &lt;CommandBinding Command='{x:Static MyCommands:TestButtonCommand.AddFruit}'
                                                  Executed='CommandBinding_Executed'
                                                  CanExecute='CommandBinding_CanExecute' /&gt;
              &lt;/Button.CommandBindings&gt;
          &lt;/Button&gt;
      &lt;/StackPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>and code behind:</p>
  
  <pre><code>namespace TestCommand
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
              MyList.ItemsSource = Fruits.getAllFruit();
          }
  
          private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
          {
              Fruits.Add("Durian", "Green");
          }
  
          private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
          {
              e.CanExecute = true;
          }
      }
  }
  </code></pre>
  
  <p>My ItemTemplate</p>
  
  <pre><code>&lt;Application x:Class="TestCommand.App"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:local="clr-namespace:TestCommand"
               StartupUri="MainWindow.xaml"&gt;
      &lt;Application.Resources&gt;
              &lt;DataTemplate x:Key='FruitTemp'&gt;
                  &lt;StackPanel Orientation='Horizontal'
                                          Margin='5'&gt;
                      &lt;TextBlock x:Name='tbName'
                                           Text='{Binding FruitName}' 
                                           Margin='10,0,0,0'
                                           Width='100'/&gt;
                      &lt;TextBlock x:Name='tbColor'
                                       Text='{Binding FruitColor}'
                                       Margin='10,0,0,0'
                                       Width='100' /&gt;
                      &lt;!--&lt;CheckBox x:Name='cbSelected'
                                          Content='Selected'
                                          Checked='{Binding bSelected}' /&gt;--&gt;
                  &lt;/StackPanel&gt;
              &lt;/DataTemplate&gt;
      &lt;/Application.Resources&gt;
  &lt;/Application&gt;
  </code></pre>
  
  <p>When I clicked on the button, I saw item added to the collection but the collection was not refreshed on the list. I must have not tied the collection correctly or may have missed something as I am very new to wpf.</p>
  
  <p>Your help to point out my overlook is very much appreciated.</p>