0

私はMVVMパターンを使用しています。私のViewModelには、パブリックなObservableCollectionがあります:

public ObservableCollection<SettingsTemplateHistoryItemViewModel> HistoryItemCollection;

public SettingsTemplateViewModel()
    {
        this.HistoryItemCollection = new ObservableCollection<SettingsTemplateHistoryItemViewModel>();
    }

私のビューには、ViewModel の ObservableCollection にバインドされた ItemsSource プロパティを持つ ItemsControl があります。

<ItemsControl ItemsSource="{Binding HistoryItemCollection}"/>

次のエラーが表示されます。

BindingExpression path error: 'HistoryItemCollection' property not found on 'object' ''SettingsTemplateViewModel' (HashCode=48413709)'. BindingExpression:Path=HistoryItemCollection; DataItem='SettingsTemplateViewModel' (HashCode=48413709); target element is 'ItemsControl' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')

私は困惑しています。プロパティがビューの DataContext (つまり、ViewModel) に存在することは 100% 確信しています。プロパティ名をコピーしてビューに貼り付けたので、バインディング パスは正しいはずです。View と ViewModel は、暗黙的/型指定された DataTemplates を介して接続されます。私は何が欠けていますか?

4

1 に答える 1

6

バインディング エラーが示すように、アイテム コントロールのデータ コンテキストには、HistoryItemCollection というパブリック プロパティが含まれていません。実行時にデータコンテキストをチェックする簡単な方法は、Snoopを使用することです

編集: フィールドの代わりにプロパティを使用する必要があります。

public ObservableCollection<SettingsTemplateHistoryItemViewModel> HistoryItemCollection {get;set;}
于 2013-01-31T07:41:35.533 に答える