title: Binding/DataContext Issue with ItemSource in WPF using MVVM
body: <p>I have a ViewModel that is a <code>Window</code>, inside this Window there are many <code>UserControl</code>s that I have made. These work fine and the bindings and <code>DataContext</code>s for each is set appropriately; all apart from one... </p>
  
  <p>In my <code>MainWindowView</code> XAML I have </p>
  
  <pre><code>&lt;Controls:LogViewerView HorizontalAlignment="Stretch"
                          VerticalAlignment="Stretch"
                          DataContext="{Binding LogViewerViewModel}"/&gt;
  </code></pre>
  
  <p>and in my <code>MainWindowViewModel</code> I have </p>
  
  <pre><code>public LogViewerViewModel LogViewerViewModel { get; set; }
  </code></pre>
  
  <p><strong>The LogViewerView</strong></p>
  
  <pre><code>&lt;UserControl x:Class="GambitFramework.Utilities.Controls.Views.LogViewerView"
               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:Caliburn="http://www.caliburnproject.org"
               xmlns:Models="clr-namespace:GambitFramework.Utilities.Models"
               mc:Ignorable="d" 
               d:DesignHeight="300" 
               d:DesignWidth="200"&gt;
      &lt;UserControl.Resources&gt;
          &lt;ResourceDictionary&gt;
              &lt;ResourceDictionary.MergedDictionaries&gt;
                  &lt;ResourceDictionary Source="../../Resources/Styles.xaml"/&gt;
              &lt;/ResourceDictionary.MergedDictionaries&gt;
          &lt;/ResourceDictionary&gt;   
      &lt;/UserControl.Resources&gt;
      &lt;DockPanel&gt;
          &lt;ItemsControl ItemsSource="{Binding LogEntries}" 
                            Style="{StaticResource LogViewerStyle}"&gt;
              &lt;ItemsControl.Template&gt;
                  &lt;ControlTemplate&gt;
                      &lt;ScrollViewer CanContentScroll="True"&gt;
                          &lt;ItemsPresenter/&gt;
                      &lt;/ScrollViewer&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/ItemsControl.Template&gt;
              &lt;ItemsControl.ItemsPanel&gt;
                  &lt;ItemsPanelTemplate&gt;
                      &lt;VirtualizingStackPanel IsItemsHost="True"/&gt;
                  &lt;/ItemsPanelTemplate&gt;
              &lt;/ItemsControl.ItemsPanel&gt;
          &lt;/ItemsControl&gt;
      &lt;/DockPanel&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>where <code>LogViewerViewModel</code> is </p>
  
  <pre><code>public class LogViewerViewModel : PropertyChangedBase
  {
      private BindableCollection&lt;LogEntry&gt; logEntries;
  
      public LogViewerViewModel() { }
      public LogViewerViewModel(IEnumerable&lt;LogEntry&gt; logEntries)
      {
          LogEntries = new BindableCollection&lt;LogEntry&gt;(logEntries);
      }
  
      public BindableCollection&lt;LogEntry&gt; LogEntries
      {
          get { return logEntries; }
          set
          {
              logEntries = value;
              NotifyOfPropertyChange(() =&gt; LogEntries);
          }
      }
  }
  </code></pre>
  
  <p>and where in Styles.xaml we have </p>
  
  <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                      xmlns:Caliburn="http://www.caliburnproject.org" 
                      xmlns:Models="clr-namespace:GambitFramework.Utilities.Models"&gt;
      &lt;Style x:Key="LogViewerStyle" TargetType="ItemsControl"&gt;
          &lt;Setter Property="Template"&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate&gt;
                      &lt;ScrollViewer CanContentScroll="True"&gt;
                          &lt;ItemsPresenter/&gt;
                      &lt;/ScrollViewer&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
          &lt;Setter Property="ItemsPanel"&gt;
              &lt;Setter.Value&gt;
                  &lt;ItemsPanelTemplate&gt;
                      &lt;VirtualizingStackPanel IsItemsHost="True"/&gt;
                  &lt;/ItemsPanelTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
      &lt;/Style&gt;
      &lt;DataTemplate DataType="{x:Type Models:LogEntry}"&gt;
          &lt;Grid IsSharedSizeScope="True"&gt;
              &lt;Grid.ColumnDefinitions&gt;
                  &lt;ColumnDefinition SharedSizeGroup="Timestamp" Width="Auto"/&gt;
                  &lt;ColumnDefinition SharedSizeGroup="Index" Width="Auto"/&gt;
                  &lt;ColumnDefinition SharedSizeGroup="IconSource" Width="Auto"/&gt;
                  &lt;ColumnDefinition/&gt;
              &lt;/Grid.ColumnDefinitions&gt;
              &lt;TextBlock Text="{Binding Timestamp}" 
                         Grid.Column="0"
                         FontWeight="Bold" 
                         Margin="5,0,5,0"/&gt;
              &lt;TextBlock Text="{Binding Index}" 
                         Grid.Column="1"
                         FontWeight="Bold" 
                         Margin="0,0,2,0" /&gt;
              &lt;TextBlock Text="{Binding Message}" 
                         Grid.Column="3"
                         TextWrapping="Wrap"/&gt;
          &lt;/Grid&gt;
      &lt;/DataTemplate&gt;
  &lt;/ResourceDictionary&gt;
  </code></pre>
  
  <p>Where the model for <code>LogEntry</code> is</p>
  
  <pre><code>public class LogEntry : PropertyChangedBase
  {
      private uint index;
      private DateTime timestamp;
      private IconPresentor iconSource;
      private string message;
  
      public uint Index
      {
          get { return index; }
          set
          {
              index = value;
              NotifyOfPropertyChange(() =&gt; Index);
          }
      }
  
      public DateTime Timestamp
      {
          get { return timestamp; }
          set
          {
              timestamp = value;
              NotifyOfPropertyChange(() =&gt; Timestamp);
          }
      }
  
      public string Message
      {
          get { return message; }
          set
          {
              message = value;
              NotifyOfPropertyChange(() =&gt; Message);
          }
      }
  }
  </code></pre>
  
  <p>But my items are not being displayed and when I use Snoop to check the bindings</p>
  
  <blockquote>
    <p>Cannot set Expression. It is marked as 'NonShareable' and has already been used </p>
  </blockquote>
  
  <p>which clearly suggests the DataContext is not set correctly. What am I doing wrong here and why is my <code>DataContext</code> not set for my control?</p>
  
  <p>Thanks very much for your time.</p>
  
  <hr>
  
  <p><em>Edit. Here is an answer using the same log control but binding to the code behind, I want to bind to a separate file:</em> <a href="https://stackoverflow.com/a/16745054/626442">https://stackoverflow.com/a/16745054/626442</a></p>