title: Style list item based on its property
body: <p>I am pretty new to WPF and I am trying to build a quite simple application using MVVM light.</p>
  
  <p>In my MainWindow.xaml (view) I have this :</p>
  
  <pre><code>    &lt;ListBox ItemsSource="{Binding InstalledVersions}"
               ItemTemplate="{StaticResource VersionsDataTemplate}"
               Style="{StaticResource VersionsStyle}"
               ItemContainerStyle="{StaticResource VersionItemStyle}"/&gt;
  </code></pre>
  
  <p>Where InstalledVersions is a list of InstalledVersionViewModel</p>
  
  <p>In my MainWindowResources.xaml I have this (simplified) :</p>
  
  <pre><code>&lt;DataTemplate x:Key="VersionsDataTemplate"
                DataType="{x:Type viewmodels:InstalledVersionViewModel}"&gt;
      &lt;Grid&gt;
          &lt;TextBlock Text="{Binding VersionNumber}" /&gt;
          &lt;TextBlock Text="{Binding FolderPath}" /&gt;
      &lt;/Grid&gt;
  &lt;/DataTemplate&gt;
  &lt;Style x:Key="VersionsStyle"
         TargetType="{x:Type ListBox}"&gt;
      &lt;Setter Property="HorizontalContentAlignment" Value="Stretch" /&gt;
      &lt;Setter Property="VerticalContentAlignment" Value="Center" /&gt;
  &lt;/Style&gt;
  &lt;Style x:Key="VersionItemStyle"
         TargetType="{x:Type ListBoxItem}"&gt;
      &lt;Setter Property="Background" Value="White" /&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>I wish to have a different background depending on the "IsActive" property of my InstalledVersionViewModel.</p>
  
  <p>I tried to add this (as well as several variations of it) to my VersionItemStyle but (as I suspected, mostly because I don't understand what I'm doing) it doesn't work :</p>
  
  <pre><code>    &lt;Style.Triggers&gt;
          &lt;Trigger Property="{Binding Path=DataContext.IsActive, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type viewmodels:InstalledVersionViewModel}}}" Value="True"&gt;
              &lt;Setter Property="Background" Value="Red" /&gt;
          &lt;/Trigger&gt;
      &lt;/Style.Triggers&gt;
  </code></pre>
  
  <p>Thanks !</p>