title: Listbox item WPF, different background color for different items
body: <p>I have a WPF ListBox containing a binded list of items from a specific class that I have. Something like this:</p>
  
  <pre><code>    ObservableCollection&lt;MyTable&gt; tables = new ObservableCollection&lt;MyTable&gt;();
  ...
      listTables.ItemsSource = tables;
  </code></pre>
  
  <p>And the XAML:</p>
  
  <pre><code>&lt;ListBox HorizontalAlignment="Left" Margin="8,10,0,0" Name="listTables" Width="153" ItemsSource="{Binding tables}" SelectionChanged="listTables_SelectionChanged" Height="501" VerticalAlignment="Top"&gt;
          &lt;ListBox.ItemTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;Grid Margin="1"&gt;
                      &lt;TextBlock Grid.Column="1" Text="{Binding tableName}" /&gt;
                  &lt;/Grid&gt;
              &lt;/DataTemplate&gt;
          &lt;/ListBox.ItemTemplate&gt;
      &lt;/ListBox&gt;
  </code></pre>
  
  <p>All works fine. What I want to do now is have a different background for each item in the ListBox depending on a certain property of the class. For example, let's say that the MyTable class has a property called isOccupied. If this flag is set for a certain item, I want it to have a red background in the ListBox, if it's not, then I want to have it with a green background. If the property changes, then the background should change accordingly.</p>
  
  <p>Any tips on how to achieve this? I'm looking up some information regarding ItemContainerStyle at the moment but I'm relatively new to this so I'm not sure if I'm following the right path.</p>