gpt4 book ai didi

wpf - ItemsControl 与其项目源不一致 - 发生在嵌套的 ListView 中

转载 作者:行者123 更新时间:2023-12-03 10:51:48 28 4
gpt4 key购买 nike

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

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

An ItemsControl is inconsistent with its items source



我明白这个问题......附在 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 Hook 的 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

最佳答案

在 C# 中,我解决了这个问题:

Dispatcher.CurrentDispatcher.Invoke(
new Action<Note>(item => city.Notes.Add(item)),
DispatcherPriority.Background,
note);

关于wpf - ItemsControl 与其项目源不一致 - 发生在嵌套的 ListView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14470621/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com