title: How to display strings in a WPF ListView in different colours?
body: <p>I would like to fill a WPF ListView with strings using a databinding. My code looks like this (and work! ;) ):</p>
  
  <p><strong>Xaml:</strong></p>
  
  <pre><code>&lt;ListView 
              ItemsSource="{Binding Entries}"&gt;
  &lt;/ListView&gt;
  </code></pre>
  
  <p>I left out some code for a better overview.
  Entries is an <code>IList&lt;string&gt;</code>.</p>
  
  <p>So far, everything is working fine. Now comes the problem: A string in Entries may contain a particular keyword that indicates that this string wants to be displayed with red background inside the ListView. Say we have a method GetBackground(string s) which returns a Colour depending on the string. </p>
  
  <p>How can I make the ListView display its items in the correct colour. 
  My first idea was to have a converter converting my string to a colour using the abovementioned method. Where would I have to add this converter and how do I pass the string to the converter as parameter? my first Idea was:</p>
  
  <pre><code>&lt;ListView
      ItemsSource="{Binding Entries, Converter={StaticResource entryToColourConverter}, 
  ConverterParameter=???}"
  &lt;/ListView&gt;
  </code></pre>
  
  <p>Does anyone have an idea how this could be done? Am I on the right track?</p>
  
  <p>Best wishes,
  Christian</p>
  
  <p><strong>EDIT 1:</strong>
  Changed the code (as a first step) towards:</p>
  
  <pre><code>&lt;UserControl.Resources&gt;
          &lt;DataTemplate x:Key="entryTemplate"&gt;
              &lt;TextBlock 
                  Text="{Binding}"
                  Background="Green"/&gt;
          &lt;/DataTemplate&gt;
  &lt;/UserControl.Resources&gt;
  
  ...
  
  &lt;ListView 
              ItemsSource="{Binding Entries}"
              ItemTemplate="{StaticResource entryTemplate}&gt;
  &lt;/ListView&gt;
  </code></pre>
  
  <p>However, this does not work at all. Even if I change the text to some static value, the result is still the same as with the previous code.</p>
  
  <p><strong>EDIT 2:</strong>
  I found the problem, my code looked like this:</p>
  
  <pre><code>&lt;ListView x:Name="lvEntries"
                    ItemTemplate="{StaticResource EntriesTemplate}"
                    ItemsSource="{Binding Entries, NotifyOnTargetUpdated=True}"&gt;
  
              &lt;ListView.View&gt;
  
                  &lt;GridView x:Name="gvEntries"&gt;
                      &lt;GridViewColumn
                          HeaderContainerStyle="{StaticResource hcs}"
                          Header="Entry"
                          TextBlock.TextAlignment="Left"&gt;
                      &lt;/GridViewColumn&gt;
                  &lt;/GridView&gt;
              &lt;/ListView.View&gt;
  
          &lt;/ListView&gt;
  </code></pre>
  
  <p>And this ListView.View was the problem. On removing that, everything was working! :) Now I have to find out how to solve this without the ListView.View</p>