この特定のページには、特定の州のすべての都市が表示されます。ListView 内に ListView を表示しています。外側の ListView には、都市のリストが表示されます。内側の ListView には、特定の各都市に添付されたすべてのメモが表示されます。
すべてが適切に読み込まれます。データベースに追加されるメモを追加できます。ただし、メモを追加した後、NotesListView でスクロール バーをクリックすると、例外が発生します。
ItemsControl が項目ソースと矛盾しています
問題を理解しています...ビューモデルのcity.Notesプロパティにメモを追加した後、ListViewに添付されたメモのリストが同期しなくなります...しかし、このListViewを強制的に更新するにはどうすればよいですか?
これが私のXAMLです(簡潔にするために編集されています):
<ListView x:Name="CityListView" ItemsSource="{Binding CityDetails, Mode=OneWay}">
<!--City Name and other city details. go here-->
<ListView x:Name="NotesListView" ItemsSource="{Binding Notes}">
<TextBlock Text="{Binding Path=Note}" />
</ListView>
<StackPanel>
<TextBlock Text="Add Note:" />
<TextBox Text="{Binding Path=DataContext.Note, RelativeSource={RelativeSource AncestorType=UserControl}}" />
<Button Content="ADD NOTE" Command="{Binding Path=DataContext.AddNoteCommand, RelativeSource={RelativeSource AncestorType=UserControl}}" CommandParameter="{Binding}" />
</StackPanel>
</ListView>
AddNoteCommand にフックされた ViewModel の AddNote() メソッドを次に示します。
Protected _stateService As IStateService
Protected _state As State
Protected Sub OnAddNote(city As City)
Dim note As Note = Nothing
If Not String.IsNullOrWhiteSpace(Me.Note) Then
note = New Note() With {
.Note = Me.Note,
.NoteDate = DateTime.Now,
}
city.Notes.Add(note)
_stateService.SaveExistingState(_state)
' this saves the note, since _state contains:
' Property Cities As ICollection(Of City)
' and the city object passed into this method belongs to that collection...
RaisePropertyChanged(Function() city.Notes)
End If
End Sub