Binding multiple views to a single BO

Binding multiple views to a single BO

Old forum URL: forums.lhotka.net/forums/t/11928.aspx


c_manboy posted on Tuesday, April 09, 2013

I'm doing some multiple bindings to a single BO in WPF and would appreciate some feedback in case I'm going down a bad path.

I have created a "ViewModelConductor(of M)" which will import child "ViewModel(of M)".  The conductor will be responsible for the fetch/save/cancel/etc. and for setting the child ViewModel's model property. So far everything works very well.  Fetch sets all child model properties, and save calls ApplyEdit on all child model properties followed by a beginsave on the conductor model. 

(I should add that my viewmodel(of M) is cslacontrib screenwithmodel(of M) for use with caliburn micro.  And my conductor is really a modification of CM conductor class which conducts the screenwithmodel class and contains its own model property).

My issue is with Cancel.  Depending on various combinations of canceledit and beginedit, cancel will either work periodically or fail outright.  Either way it fails to work consistently.

I read this post which states that n-level undo is unsupported in these scenarios.  But in this post I read that v4 supports multiple binding scenarios.

My current workaround is this method on my conductor DoCancel override:

          For Each item In Me.Items
                    If item.IsScreenCreated Then
                        Dim screen = TryCast(item.Screen, ScreenWithModel(Of TModel))
                        If screen IsNot Nothing AndAlso screen.Model IsNot Nothing Then
                            Dim undoable = TryCast(screen.Model, ISupportUndo)
                            If undoable IsNot Nothing Then
                                undoable.CancelEdit()
                            End If
                            screen.Model = Nothing
                        End If
                    End If
                Next
                'call canceledit and beginedit on conductor model
                MyBase.DoCancel()
               'set children model property to conductor model
               ChangeModel(NothingMe.Model)

This code seems to work, but I would appreciate any feedback concerning this process.

Copyright (c) Marimer LLC