此给定页面显示特定州的所有城市。我在ListView中显示ListView。外部ListView显示城市列表。内部ListView显示附加到每个特定城市的所有注释。
一切都正常加载。我可以添加一个注释,它会被添加到数据库中。但是,添加注释后,单击NotesListView中的滚动条会导致异常:
ItemsControl与其商品来源
不一致
我理解这个问题...在我向viewmodel中的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>
以下是ViewModel中的AddNote()方法,该方法挂钩到AddNoteCommand:
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
答案 0 :(得分:-1)
在C#中我解决了这个问题:
Dispatcher.CurrentDispatcher.Invoke(
new Action<Note>(item => city.Notes.Add(item)),
DispatcherPriority.Background,
note);