title: WPF Bind Background of ListView ItemContainerStyle
body: <p>I have a <code>UserControl</code> (<code>FahrtControl.xaml</code>) with a <code>ListView</code>. This <code>UserControl</code> is bound to an <code>ItemsControl</code> in my <code>MainWindow.xaml</code>. The <code>MainWindow</code> has its own ViewModel and the <code>FahrtControl</code> has its own ViewModel. I now want to bind the Background of the <code>Listview</code> items to a <code>Brush</code> property in the ViewModel of <code>FahrtControl</code>.</p>
  
  <p>Here are the relevant parts of my code:</p>
  
  <p>MainWindow.xaml:</p>
  
  <pre><code>&lt;Window x:Class="WpfFrontend.Forms.Main.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:WpfFrontend.Forms.Main"
      xmlns:fahrtControl="clr-namespace:WpfFrontend.Controls.FahrtControl"
      mc:Ignorable="d"&gt;
  &lt;Window.DataContext&gt;
      &lt;local:MainViewModel /&gt;
  &lt;/Window.DataContext&gt;
  &lt;Window.Resources&gt;
      &lt;DataTemplate DataType="{x:Type fahrtControl:FahrtControlViewModel}"&gt;
          &lt;fahrtControl:FahrtControl /&gt;
      &lt;/DataTemplate&gt;
  &lt;/Window.Resources&gt;
  &lt;ItemsControl ItemsSource="{Binding Fahrten, UpdateSourceTrigger=PropertyChanged}" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>MainViewModel.cs:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Collections.ObjectModel;
  using System.ComponentModel;
  using System.Linq;
  using System.Runtime.CompilerServices;
  using System.Windows.Controls;
  using System.Windows.Media;
  
  using Backend;
  
  using DatabaseCommunication;
  
  using WpfFrontend.Annotations;
  using WpfFrontend.Controls.FahrtControl;
  
  
  namespace WpfFrontend.Forms.Main
  {
      public class MainViewModel : INotifyPropertyChanged
      {
          public MainViewModel ()
          {
              SyncFahrten ();
          }
  
          private void SyncFahrten ()
          {
              var fahrtenPromise =
                  MainUtility.GetFahrtenToRangeAsync (GlobalProperties.Start, GlobalProperties.Start.AddDays (6));
  
              fahrtenPromise.Task.GetAwaiter ().OnCompleted (() =&gt;
              {
                  AddFahrten (fahrtenPromise.Task.Result);
              });
          }
  
          private void AddFahrten (List &lt;ExtendedFahrt&gt; fahrten)
          {
                  foreach (var fahrtControlViewModel in
                      fahrten.Select (fahrt =&gt;
                                   {
                                       return new FahrtControlViewModel (
                                           Brushes.Red, Brushes.Red, Brushes.White,
                                           fahrt.Ort,
                                           new ObservableCollection &lt;string&gt;
                                           {
                                               fahrt.Bemerkung
                                           }, new ObservableCollection &lt;KundeDisplay&gt; (fahrt.Kunden));
                                   }))
                      Fahrten.Add (fahrtControlViewModel);
  
              OnPropertyChanged ("");
          }
  
  
  
          private ObservableCollection &lt;FahrtControlViewModel&gt; _fahrten =
              new ObservableCollection &lt;FahrtControlViewModel&gt; ();
  
          public ObservableCollection &lt;FahrtControlViewModel&gt; Fahrten
          {
              get =&gt; _fahrten;
              set
              {
                  if (Equals (value, _fahrten))
                      return;
                  _fahrten = value;
                  OnPropertyChanged ();
              }
          }
  
          public event PropertyChangedEventHandler PropertyChanged;
  
          [NotifyPropertyChangedInvocator]
          protected virtual void OnPropertyChanged ([CallerMemberName] string propertyName = null) =&gt;
              PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (propertyName));
      }
  }
  </code></pre>
  
  <p>FahrtControl.xaml:</p>
  
  <pre><code>&lt;UserControl x:Class="WpfFrontend.Controls.FahrtControl.FahrtControl"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
               xmlns:local="clr-namespace:WpfFrontend.Controls.FahrtControl"
               mc:Ignorable="d"
               d:DesignHeight="300" d:DesignWidth="300"&gt;
      &lt;UserControl.Resources&gt;
          &lt;Style x:Key="HeaderStyle" TargetType="{x:Type GridViewColumnHeader}"&gt;
              &lt;Setter Property="Visibility" Value="Collapsed" /&gt;
          &lt;/Style&gt;
      &lt;/UserControl.Resources&gt;
      &lt;ListView ItemsSource="{Binding Kunden}"
                Background="{Binding KundenBrush, UpdateSourceTrigger=PropertyChanged}"&gt;
          &lt;ListView.ItemContainerStyle&gt;
              &lt;Style TargetType="ListViewItem"&gt;
                  &lt;Setter Property="Background" Value="{Binding DataContext.KundenBrush}" /&gt;
              &lt;/Style&gt;
          &lt;/ListView.ItemContainerStyle&gt;
          &lt;ListView.View&gt;
              &lt;GridView ColumnHeaderContainerStyle="{StaticResource HeaderStyle}"&gt;
                  &lt;GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name" /&gt;
              &lt;/GridView&gt;
          &lt;/ListView.View&gt;
      &lt;/ListView&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>FahrtControlViewModel.cs:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Collections.ObjectModel;
  using System.ComponentModel;
  using System.Runtime.CompilerServices;
  using System.Windows.Media;
  
  using Backend;
  
  using WpfFrontend.Annotations;
  using WpfFrontend.Misc;
  
  
  namespace WpfFrontend.Controls.FahrtControl
  {
      public class FahrtControlViewModel : INotifyPropertyChanged
      {
          private Brush                               _kundenBrush = Brushes.Red;
          private ObservableCollection &lt;KundeDisplay&gt; _kunden;
  
          /// &lt;inheritdoc /&gt;
          public FahrtControlViewModel (
              Brush                               kundenBrush,
              ObservableCollection &lt;KundeDisplay&gt; kunden)
          {
              Kunden         = kunden;
              KundenBrush    = kundenBrush;
          }
          public Brush KundenBrush
          {
              get =&gt; _kundenBrush;
              set
              {
                  if (value.Equals (_kundenBrush))
                      return;
                  _kundenBrush = value;
                  KundenDark   = _kundenBrush.IsDark ();
  
                  OnPropertyChanged ();
              }
          }
  
          public ObservableCollection &lt;KundeDisplay&gt; Kunden
          {
              get =&gt; _kunden;
              set
              {
                  if (Equals (value, _kunden))
                      return;
                  _kunden = value;
                  OnPropertyChanged ();
              }
          }
  
  
          public event PropertyChangedEventHandler PropertyChanged;
  
          [NotifyPropertyChangedInvocator]
          protected virtual void OnPropertyChanged ([CallerMemberName] string propertyName = null) =&gt;
              PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (propertyName));
      }
  }
  </code></pre>
  
  <p>I've already tried the following:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/16126393/how-i-change-listview-item-background-color-according-to-listview-item-harfnotu?utm_medium=organic&amp;utm_source=google_rich_qa&amp;utm_campaign=google_rich_qa">How i change ListView Item Background Color according to listview item HarfNotu value in wpf</a></li>
  <li><a href="https://social.msdn.microsoft.com/Forums/vstudio/en-US/b25973bb-9e9c-4a99-8234-39a042e0a478/apply-styles-dynamically-to-buttons-in-xaml?forum=wpf" rel="nofollow noreferrer">https://social.msdn.microsoft.com/Forums/vstudio/en-US/b25973bb-9e9c-4a99-8234-39a042e0a478/apply-styles-dynamically-to-buttons-in-xaml?forum=wpf</a></li>
  <li><a href="https://stackoverflow.com/questions/35507194/how-do-i-set-the-background-color-of-a-listview-item-in-wpf-using-databinding/35507355?utm_medium=organic&amp;utm_source=google_rich_qa&amp;utm_campaign=google_rich_qa">How do I set the background color of a listview item in WPF using databinding?</a></li>
  <li><a href="https://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.contenttemplateselector%28v=vs.110%29.aspx" rel="nofollow noreferrer">https://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.contenttemplateselector%28v=vs.110%29.aspx</a></li>
  <li><a href="https://social.msdn.microsoft.com/Forums/vstudio/en-US/4df9f644-9bfa-4913-acc6-0bce711b70ec/setting-wpf-listview-background-when-disabled-and-empty?forum=wpf" rel="nofollow noreferrer">https://social.msdn.microsoft.com/Forums/vstudio/en-US/4df9f644-9bfa-4913-acc6-0bce711b70ec/setting-wpf-listview-background-when-disabled-and-empty?forum=wpf</a></li>
  <li><a href="https://stackoverflow.com/questions/17200771/binding-the-value-of-a-setter-property-in-wpf?utm_medium=organic&amp;utm_source=google_rich_qa&amp;utm_campaign=google_rich_qa">Binding the value of a Setter Property in WPF</a></li>
  <li><a href="https://stackoverflow.com/questions/16173869/wpf-error-40-bindingexpression-path-error-property-not-found-on-object?utm_medium=organic&amp;utm_source=google_rich_qa&amp;utm_campaign=google_rich_qa">WPF Error 40 BindingExpression path error: property not found on &#39;object&#39;</a></li>
  <li><a href="https://stackoverflow.com/questions/7130941/how-to-set-background-of-listview">How to set background of listview?</a></li>
  </ul>
  
  <p>And if I had to guess every other suggestion slightly related to the topic. What am I doing wrong here? Does it have something to do with the fact that I'm using a UserControl inside an ItemsControl? Other property bindings, not enclosed by a style tag, work inside the my <code>UserControl</code>, so it must have something to do with the style tag, doesn't it?</p>