0

I have a project implementing a Mediator pattern similar to what is found in this question: Sql, Wpf, Xaml, C#, Binding data, Dynamic resource, accessing to non-static data, Obtaining a Reference to an Object

This seems to work fine when the Register() function is called in the parent view then the SendMessage() function in a child view. The data can easily be obtained that is acquired in the child and then the parent can process it through the Mediator.

However I need to do the opposite. Take data acquired in the parent and pass it to the child to be processed. Can the Mediator be used for this task?

I have tried various methods of placing a call to the Register() function in the child with a SendMessage() in the parent, but since the Register() initializes the collection the records seems to be getting lost.

Am I missing something? Is there a better way?

Community
  • 1
  • 1
DJ.
  • 1,005
  • 2
  • 15
  • 24
  • Its essentially whats in the link except the SelectList.xaml code-behind and the Temp.xaml code-behind are switched. – DJ. Jan 12 '12 at 21:45

1 Answers1

0

I had the same problem... i know its not a good Solution but i solved it like this....

In Your ChildView

    public ChildViewModel()
    {
        Messenger.UnRegister(this); //I use reflection and Attributes to register/Unregister you can do it normally
        Messenger.Register(this);
        if (ChildData== null)
        {
            Messenger.NotifyColleagues<object>(
                MessengerMessages.GET_CHILD_DATA,ChildData);
        }            
    }
    [MessengerMessageSink(MessengerMessages.SEND_CHID_DATA,
        ParameterType = typeof (CHILD_DATA))]
    protected void Set_Child_DATA(ChildData childData)
    {
        if (childData!= null)
        {
            //Do Something              
        }
    }

In your ParentView

    public ParentViewModel()
    {
        Messenger.UnRegister(this); //I use reflection and Attributes to register/Unregister you can do it normally
        Messenger.Register(this);            
    }
    [MessengerMessageSink(MessengerMessages.GET_CHILD_DATA,
        ParameterType = typeof (CHILD_DATA))]
    protected void Send_Child_DATA(Object obj)
    {
                        Messenger.NotifyColleagues<object>(
                MessengerMessages.SEND_CHILD_DATA,ChildData);
    }

Here we are calling the parentViewModel to send the required data when the ChildViewModel dosent find the Data it requires....

Ankesh
  • 4,847
  • 4
  • 38
  • 76
  • What is going on with this call? [MessengerMessageSink(MessengerMessages.GET_CHILD_DATA, ParameterType = typeof (CHILD_DATA))] – DJ. Jan 20 '12 at 21:18
  • @DJ. I use reflection to register Messages.... the above line in the attribute given to the Function so that its registers function for a call to the message `MessengerMessages.GET_CHILD_DATA` – Ankesh Jan 23 '12 at 04:22