構文を使用して、ViewModel のプロパティにバインドされListBox
た ItemContainer のプロパティがあります。IsSelected
IsSelected
<ListBox.ItemContainerStyle>
正常に動作しますが、Resharper の警告が表示されます。
タイプ「FooSolution.BarViewModel」のデータ コンテキストでプロパティ「IsSelected」を解決できません。
この警告を取り除くには、ListBox ItemContainer で DataContext タイプを指定するにはどうすればよいですか?
これがコードです。私はBarViewModel
クラスを持っています:
public ObservableCollection<FooViewModel> FooItems { get;set; }
BarViewModel
ListBox を含むコントロールの DataContext に割り当てられます
そしてFooViewModel
次のように:
public bool IsSelected
{
get
{
return isSelected;
}
set
{
if (isSelected == value)
{
return;
}
isSelected = value;
RaisePropertyChanged(() => IsSelected);
}
}
XAML は次のようになります。
<ListBox ItemsSource="{Binding FooItems}" SelectionMode="Multiple">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
更新HighCore で提案されているように、セッターを使用して
設定しようとしましd:DataContext
たが、残念ながら、それは役に立たず、ビルドを壊すことさえあります:
<Setter Property="d:DataContext" Value="{d:DesignInstance yourxmlns:yourItemViewModelClass}"/>
(スロー: エラー 1 The tag 'DesignInstance' does not exist in XML namespace 'schemas.microsoft.com/expression/blend/2008';. Line 31 Position 50. )
更新 2
最後に、解決策はスタイル要素自体に設定d:DataContext
することです (下記の私の回答を参照してください)。
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}" d:DataContext="{d:DesignInstance local:FooViewModel }">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
</Style>