gpt4 book ai didi

.net - 从 ViewModel 中的 Observable 集合中移除不会更新 View,但 Updates of Existing Items 会更新 View

转载 作者:行者123 更新时间:2023-12-04 23:54:42 30 4
gpt4 key购买 nike

我敢肯定这对某人来说将是一次扣篮……祈祷

我的 ListView ItemsSource 绑定(bind)到我的 ViewModel 上名为 TileItems 的属性。

填充 ListView 会完美更新。

在 ViewModel 中,您会看到 "existingTileItem.Transaction = e.Transaction"。 . .单个 ListView 项目完美更新。

在 ViewModel 中,您会看到“Me.TileItems.Remove(existingTileItem)”...该项目不会从 View 中删除。它确实从 Me.TileItems 集合中成功删除,但 View 中未显示更新。

附加信息:AbstractViewModel 实现了 INotificationPropertyChanged,我尝试在 TileItem 中覆盖 Equals 而不是覆盖它,并且周围发生了相同的结果。我看过this回答和this回答,但他们没有回答我遇到的问题。

XAML:

<UserControl.DataContext>
<local:TransactionTileResultsViewControlViewModel />
</UserControl.DataContext>

<ListView Grid.Row="1" Name="tileItems" ItemsSource="{Binding TileItems, Mode=TwoWay}"
ItemTemplate="{StaticResource tileItemDataTemplate}" ScrollViewer.HorizontalScrollBarVisibility="Hidden"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />

View 模型:
Public Class TransactionTileResultsViewControlViewModel
Inherits AbstractViewModel
Implements INavigationAware

Private _tileItems As TileItems
Public Property TileItems As TileItems
Get
Return Me._tileItems
End Get
Set(value As TileItems)
Me._tileItems = value
MyBase.RaisePropertyChanged("TileItems")
End Set
End Property

'....


#Region "TransactionUpdateReceived Methods"

Private Sub TransactionUpdateReceived_Handler(ByVal e As TransactionUpdatedEvent)

If e.Transaction IsNot Nothing Then

Dim existingTileItem As TileItem = Me.TileItems.Where(Function(t) t.Transaction.TransactionQueueID = e.Transaction.TransactionQueueID).FirstOrDefault()
If existingTileItem IsNot Nothing Then

If e.Transaction.Canceled Then

Me.TileItems.Remove(existingTileItem)

Else

If e.Transaction.ContainsFailedActivites() OrElse e.Transaction.ContainsCallbackActivities() Then

existingTileItem.Transaction = e.Transaction

Else

Me.TileItems.Remove(existingTileItem)

End If

End If

End If

End If

End Sub

#End Region

End Class

瓷砖项目型号:
Public Class TileItems
Inherits ObservableCollection(Of TileItem)

End Class

瓷砖项目型号:
 Imports Microsoft.Practices.Prism.ViewModel

Public Class TileItem
Inherits NotificationObject

Private _created As Date
Public Property Created As Date
Get
Return _created
End Get
Set(value As Date)
_created = value
MyBase.RaisePropertyChanged("Created")
End Set
End Property

Private _category As String
Public Property Category As String
Get
Return _category
End Get
Set(value As String)
_category = value
MyBase.RaisePropertyChanged("Category")
End Set
End Property

Private _tileField1 As String
Public Property TileField1 As String
Get
Return _tileField1
End Get
Set(value As String)
_tileField1 = value
MyBase.RaisePropertyChanged("TileField1")
End Set
End Property

Private _tileField2 As String
Public Property TileField2 As String
Get
Return _tileField2
End Get
Set(value As String)
_tileField2 = value
MyBase.RaisePropertyChanged("TileField2")
End Set
End Property

Private _tileField3 As String
Public Property TileField3 As String
Get
Return _tileField3
End Get
Set(value As String)
_tileField3 = value
MyBase.RaisePropertyChanged("TileField3")
End Set
End Property

Private _transaction As Transaction
Public Property Transaction As Transaction
Get
Return _transaction
End Get
Set(value As Transaction)
_transaction = value
MyBase.RaisePropertyChanged("Transaction")
End Set
End Property

Public Overrides Function Equals(obj As Object) As Boolean

If TypeOf obj Is TileItem Then

Dim tileItem As TileItem = DirectCast(obj, TileItem)

If tileItem.Transaction IsNot Nothing AndAlso Me.Transaction IsNot Nothing Then

Return tileItem.Transaction.TransactionQueueID = Me.Transaction.TransactionQueueID

Else
Return False

End If

Else
Return False
End If

End Function


End Class

更新:

根据@ReedCopsey 的回答,这是我为使其正常工作而进行的更新。

我现在将 Me.TileItems.Remove(existingTileItem) 更新为
Me.View.Dispatcher.Invoke(Sub()
Me.TileItems.Remove(existingTileItem)
End Sub, DispatcherPriority.ApplicationIdle)

最佳答案

据我所知,您粘贴的代码应该可以工作。最可能的罪魁祸首是您的TransactionUpdateReceived在不是用户界面线程的线程上引发事件。在 WPF 中,可以在后台线程上修改单个项目,但不能修改集合(在 .NET 4.5 之前,但在 .NET 4.5 中,它们需要额外的工作)。

有两种选择。如果您使用的是 .NET 4.5,则可以使用 BindingOperations.EnableCollectionSynchronization 允许 ObservableCollection从后台线程修改。

或者,您可以使用 Dispatcher.Invoke将 Add/Remove 调用推送到主线程。

关于.net - 从 ViewModel 中的 Observable 集合中移除不会更新 View,但 Updates of Existing Items 会更新 View,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18579520/

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