1

How can I bind/ connect to different view, that have their own viewModel together?

I have a mainWindow that that contains of to user controls/ views. Each of these have there own viewModel. The first view is like a control panel, with a form to type in input parameters to the application. The other one is a canvas to show the result of the analysis based on the input parameters on the control panel.

Example:

MainWindow:

    <StackPanel Orientation="Horizontal" Margin="0,20,0,0">
        <local:ControlView />
        <local:CanvasView />
    </StackPanel>

ControlView:

        <StackPanel Orientation="Horizontal">
             <Label Content="Length: " Margin="19,0,0,0"/>
             <TextBox Margin="3" Width="130" Text="{Binding Path=Box.Length}"/>
        </StackPanel>

        <StackPanel Orientation="Horizontal">
            <Button Margin="10" Content="Draw Canvas" Command="{Binding Path=DrawCanvasCmd}"/>
        </StackPanel>

The button triggers a method called DrawCanvas in the ControlViewModel. Then the method is called, I want the canvas to be drawn, based on the input. The drawing of the canvas is done in the CanvasViewModel.

Can anyone see how this can be done? I'm able to draw the canvas using static values in contructor of CanvasViewModel i.e. So _I just need to like send the input parameters from the controlpanel to the canvas.

public void CanvasViewModel()
{
     GeometryFigure.length = 120;
}
user2590683
  • 163
  • 1
  • 1
  • 9

2 Answers2

1

If you are using MVVM light, you want to use the Messenger, held in GalaSoft.MvvmLight.Messaging.

Basic Tutorial:

In Main Window View Model assuming length is an integer

private void DrawCanvas()
{
     Messenger.Default.Send(Length, "SOME KIND OF UNIQUE TOKEN");
}

In constructor for CanvasViewModel

public void CanvasViewModel()
{
     Messegner.Default.Register<int>(this, "SOME KIND OF UNIQUE TOKEN", Draw);
}

Somewhere else in CanvasViewModel create the method you are calling in register:

private void Draw(int length)
{
    Do whatever you want to do with the length....
}

The "SOME KIND OF UNIQUE TOKEN" can be anything, but be aware any Messenger you have registered with the same token will fire if you use something too Generic. Personally I use Enumerators. Also, when you register, make sure it is for the correct type you are sending.

What this does is send a notification off, with the value and the token that was sent. If anything is registered to receive that type of value, with that exact token, it will call the method that it is registered with. This is perfect for testing too since you can register in your test method to make sure the event fired, and was received, without having to involve the view.

Daryl Behrens
  • 643
  • 2
  • 7
  • 19
0

You can use EventAggregator if you using PRISM framework, of you can make you own EventAggregator which is bases on Mediator Pattern. you can fine the same at Here and Here

Community
  • 1
  • 1
Firoz
  • 7,224
  • 10
  • 41
  • 56
  • I'm using MVVM light, and I'm very new to this. So I have never heard of either EventAggregator or Mediator Pattern. Is it difficult to learn/ implement? Or do you know of a small code sample or something that can lead me in the right direction – user2590683 Nov 03 '13 at 14:42