1

此给定页面显示特定州的所有城市。我在 ListView 中显示 ListView。外部 ListView 显示城市列表。内部 ListView 显示附加到每个特定城市的所有注释。

一切正常加载。我可以添加一个注释,它被添加到数据库中。但是,添加注释后,点击 NotesListView 中的滚动条会导致异常:

ItemsControl 与其项目源不一致

我理解这个问题......附在 ListView 上的笔记列表变得不同步,在我向 viewmodel 中的 city.Notes 属性添加一个笔记后......但是我如何强制这个 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 中与 AddNoteCommand 挂钩的 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
4

1 回答 1

-1

In C# I solved this with:

Dispatcher.CurrentDispatcher.Invoke(
    new Action<Note>(item => city.Notes.Add(item)),
    DispatcherPriority.Background,
    note);
于 2013-08-12T08:52:48.650 回答