首先嚐試在業務線項目上實現MVVM模式。我遇到了一些問題,我假設有這樣的eaiser答案:WPF MasterDetail ViewModel延遲更新到主列表。 (防止網格自動更新)
原型窗口是項目列表的基本主從視圖。 (Person對象列表)。該視圖包含主列表的Infragistics xamDataGrid。由於在網格中選擇了該項目,因此您可以在下面的詳細信息面板中編輯詳細信息,並在詳細信息面板中標註字段時更新顯示在網格數據中的「實時」。唯一的事情是我不想「presto」,我想要「等到我按下」Apply Changes「按鈕」。
我一直希望避免創建一個單獨的列表實例來從我在細節面板中添加/刪除/修改的項目的工作集中分離出主列表。
我已經下降的路徑:
我推翻了CellValuePresenter風格在網格領域,所以我可以設置綁定爲「單向」。這阻止了實時更新。
<ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}">
<ControlTemplate.Resources>
<Style TargetType="TextBlock">
<Setter Property="Background" Value="{Binding Path=DataItem.NameUIProperty.IsDirty, Converter={StaticResource BooleanBrushConverter}}" />
<Setter Property="IsEnabled" Value="{Binding Path=DataItem.NameUIProperty.IsEditable}" />
</Style>
</ControlTemplate.Resources>
<ContentControl>
<TextBlock Text="{Binding Path=DataItem.Name, Mode=OneTime}" />
</ContentControl>
</ControlTemplate>
然後我添加一個「的ApplyUpdates」命令(RelayCommand)我PersonListViewModel。這引發了「PERSON _ITEM_
UPDATED」消息。我正在使用MVVM Foundation Messenger和RelayCommand類的VB端口。
#Region "ApplyUpdates Command"
Private mApplyUpdatesCommand As New RelayCommand(AddressOf ApplyUpdates)
Public ReadOnly Property ApplyUpdatesCommand() As ICommand
Get
Return mApplyUpdatesCommand
End Get
End Property
Private Sub ApplyUpdates()
'the changes are already in the object in the list so we don't have to do anything here except fire off the Applied message
Messages.AppMessenger.NotifyColleagues(Messages.PERSON_ITEM_UPDATED)
End Sub
#End Region
的PersonView登記該PERSON _ITEM_
更新的消息和接收到消息時重新綁定網格。
'In Loaded Event
'register for window messages we care about
Messages.AppMessenger.Register(Messages.PERSON_ITEM_UPDATED, AddressOf OnPersonItemUpdated)
'EventHandler
Private Sub OnPersonItemUpdated()
PersonGrid.DataSource = Nothing
PersonGrid.DataSource = mViewModel.List
End Sub
所以,那有效,但它smells wrong。該視圖似乎有太多的邏輯,並且ViewModel沒有規定UI的狀態,視圖是。
我錯過了什麼?你會用什麼方法讓ViewModel延遲發佈視圖的更改?
更新:我現在正在爲網格創建自定義ViewModel的路徑(只讀,沒有Propertychanged通知)和可編輯的ViewModel的細節區域。兩個VM都會包裝相同的業務對象,但ReadOnly版本不會發布更改。這會讓虛擬機控制視圖更新的時間。
感謝Infragistics提示。有一天這將派上用場! – TheZenker 2009-11-30 19:44:10