如何通过委托更新通知属性并更新UI

时间:2013-02-13 19:00:41

标签: wpf events mvvm delegates caliburn.micro

我在wpf应用程序中使用了caliburn micro,其中我有一个列表和详细视图模型及其视图并排。从左侧列表中选择一个项目,并在详细信息vm中查看右侧的详细信息。

如果详细信息IsDirty(已更改,未保存)并且他们从列表中选择了新项目,我想通知用户该事实。我工作得很好。但是,如果用户单击“否”以保留其脏项,我希望列表视图保留在其当前项目上。这是我到目前为止所做的:

ListViewModel

    Private _selectedItem As Library.VEntityStatusInfo
    Public Property SelectedItem As Library.VEntityStatusInfo
        Get
            Return _selectedItem
        End Get
        Set(value As Library.VEntityStatusInfo)
            Events.Publish(New SelectionChangingEvent With {.Sender = Me,
                                                            .Identification = value.Identification,
                                                            .Callback = Sub(id As Integer)
                                                                            _selectedItem = (From m In Model Where m.Identification = id).FirstOrDefault
                                                                            NotifyOfPropertyChange(Function() SelectedItem)
                                                                        End Sub})
        End Set
    End Property

DetailViewModel

Public Sub Handle(message As SelectionChangingEvent) Implements IHandle(Of SelectionChangingEvent).Handle
    If TryCast(message.Sender, EntityList.EntityListViewModel) Is Nothing Then Return

    If Me.Model Is Nothing OrElse Me.Model.Identification <> message.Identification Then
        CanChange(message.Identification, message.Callback)
    End If
End Sub

Private Sub CanChange(identification As Integer, eventCallback As System.Action(Of Integer))
    If Me.Model IsNot Nothing AndAlso Me.Model.IsDirty Then
        Dialogs.ShowMessageBox(
            "You have unsaved data.  Are you sure you want to change employee's?  All changes will be lost.",
            "Unsaved Changes",
            MessageBoxOptions.YesNo,
            Sub(box)
                If box.WasSelected(MessageBoxOptions.Yes) Then
                    If String.IsNullOrEmpty(identification) Then
                        Me.Model = Nothing
                        Me.OnRefreshed()
                    Else
                        BeginRefresh("GetByIdentificationAsync", identification)
                    End If
                    eventCallback(identification)
                Else
                    eventCallback(Model.Identification)
                End If
            End Sub)
    Else
        eventCallback(identification)
        BeginRefresh("GetByIdentificationAsync", identification)
    End If
End Sub

SelectedItem绑定到ListBox SelectedItem并且正常运行。当我在每个步骤中放置断点时,它们都被击中,包括Get之后的属性NotifyOfPropertyChanged。但UI无法更新。

0 个答案:

没有答案