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