MVVM - Where to Send Commands


  • Consider a structure of ViewModels like this:

                                ParentVM 1
        ChildParentVM 1                            ChildParentVM 2
    ChildVM1         ChildVM2               ChildVM3          ChildVM4

    So there is one main "ParentVM 1" that has the children "ChildParentVM 1" and "ChildParentVM 2". The "ChildParentVMs" in return each have two children. The real structure is actually a bit more complicated (more than just 3 level deep and also much wider). Each of the "Children" and "ChildParents" has the commands CreateChild to create a new child, AddChild to add an existing child to the collection, DeleteChild to delete a child and RemoveChild to remove a child but not delete it.

    Now I have a custom view that implements all the communication and synchronization with the VMs. Imagine the user tells this view to move "ChildVM 1" from "ChildParentVM 1" to "ChildParentVM 2". There are a couple of ways I can think of doing this:

    1) Have a command MoveChild on the root parent "Parent 1". Add a property "RootParent" to each child. Child1 will then call Parent.MoveChild to invoke the MoveChild command on Parent 1. Parent 1 then has to go up the chain to find the child to be removed, remove it using the command RemoveChild, then find the new parent and call the command AddChild.

    2) Add a command MoveChild to all ChildParentVMs and ChildVMs. Then call MoveChild on ChildParentVM1. That parent will call RemoveChild on itself (maybe flag this as done) and then let the command bubble up. When the message comes up to the root parent Parent1 it will send it back down to ChildParent1 and ChildParent2. Each of the ChildParents will check if it is the new target parent and if so add the child by calling AddChild on itself (note that the "Parent1" doesn't necessarily have direct access to the target parent as the real structure is more than 3 levels deep so the command has to trickle down).

    What is the better approach and what alternative approaches are there? I understand there are some messaging frameworks but if possible I like to avoid them unless it's a big advantage using them.

    Friday, March 09, 2018 1:51 AM

All replies

  • Hi hfaun,

    Use routed Command as a notification that travels a route between two elements in the WPF visual tree is not my choice, though you can check to see if any element cares to react to the event, But Some elements maybe not in current visual tree, such as ContextMenu, DataGridTextColumn.

    In order to reduce the amount of code, I will put the public command encapsulation in the Parent View Model, so that my sub ViewmModel can Access all of commands easily, then establish a binding between View and ViewModel.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Friday, March 09, 2018 6:44 AM