在ListBox我有一个ItemContainer的IsSelected属性绑定到我的ViewModel的IsSelected属性使用<ListBox.ItemContainerStyle>语法.
它工作正常,但我得到一个Resharper警告:
无法在"FooSolution.BarViewModel"类型的数据上下文中解析属性"IsSelected".
如何在ListBox ItemContainer上指定指定DataContext类型以消除此警告?
这是代码.我有一BarViewModel节课:
public ObservableCollection<FooViewModel> FooItems { get;set; }
Run Code Online (Sandbox Code Playgroud)
BarViewModel 被分配给Control中包含ListBox的DataContext
并且FooViewModel,如下所示:
public bool IsSelected
{
get
{
return isSelected;
}
set
{
if (isSelected == value)
{
return;
}
isSelected = value;
RaisePropertyChanged(() => IsSelected);
}
}
Run Code Online (Sandbox Code Playgroud)
和XAML这样:
<ListBox ItemsSource="{Binding FooItems}" SelectionMode="Multiple">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
更新
我尝试d:DataContext使用setC进行设置,正如HighCore所建议的那样,但不幸的是,它没有帮助,甚至打破了构建:
<Setter Property="d:DataContext" Value="{d:DesignInstance yourxmlns:yourItemViewModelClass}"/>
Run Code Online (Sandbox Code Playgroud)
(抛出:错误1标签'DesignInstance'在XML命名空间'schemas.microsoft.com/expression/blend/2008'中不存在;;第31行位置50.)
更新2 …