title: Specify datacontext type on listbox ItemContainer in style
body: <p>In a <code>ListBox</code> I have a ItemContainer's <code>IsSelected</code> property bound to my ViewModel's <code>IsSelected</code> property using <code>&lt;ListBox.ItemContainerStyle&gt;</code> syntax. </p>
  
  <p>It works fine, but I get a Resharper warning:</p>
  
  <blockquote>
    <p>Cannot resolve property 'IsSelected' in data context of type "FooSolution.BarViewModel".</p>
  </blockquote>
  
  <p>How do I specify specify DataContext type on ListBox ItemContainer to get rid of this warning?</p>
  
  <p>Here is the code. I have a <code>BarViewModel</code> class:</p>
  
  <pre><code>public ObservableCollection&lt;FooViewModel&gt; FooItems { get;set; }
  </code></pre>
  
  <p><code>BarViewModel</code> is assigned to DataContext in the Control which contains the ListBox</p>
  
  <p>and <code>FooViewModel</code> as follows:</p>
  
  <pre><code>public bool IsSelected
  {
      get
      {
          return isSelected;
      }
  
      set
      {
          if (isSelected == value)
          {
              return;
          }
  
          isSelected = value;
          RaisePropertyChanged(() =&gt; IsSelected);
      }
  }
  </code></pre>
  
  <p>and XAML like this:</p>
  
  <pre><code>&lt;ListBox ItemsSource="{Binding FooItems}" SelectionMode="Multiple"&gt;        
      &lt;ListBox.ItemContainerStyle&gt;
          &lt;Style TargetType="{x:Type ListBoxItem}"&gt;
              &lt;Setter Property="IsSelected" Value="{Binding IsSelected}" /&gt;
          &lt;/Style&gt;
      &lt;/ListBox.ItemContainerStyle&gt;
  &lt;/ListBox&gt;
  </code></pre>
  
  <p><strong>Update</strong>
  I've tried setting <code>d:DataContext</code> using a setter, as suggested by HighCore, but unfortunatelly, it doesn't help and even breaks the build:</p>
  
  <pre><code>&lt;Setter Property="d:DataContext" Value="{d:DesignInstance yourxmlns:yourItemViewModelClass}"/&gt;
  </code></pre>
  
  <p>(Throws: Error  1   The tag 'DesignInstance' does not exist in XML namespace 'schemas.microsoft.com/expression/blend/2008';. Line 31 Position 50. )</p>
  
  <p><strong>Update 2</strong>
  Finaly, the solution is to set <code>d:DataContext</code> on the style element itself (see my answer bellow):</p>
  
  <pre><code>&lt;ListBox.ItemContainerStyle&gt;
      &lt;Style TargetType="{x:Type ListBoxItem}" d:DataContext="{d:DesignInstance local:FooViewModel }"&gt;
          &lt;Setter Property="IsSelected" Value="{Binding IsSelected}" /&gt;
      &lt;/Style&gt;
  </code></pre>
  
  <p></p>