user3182266
user3182266

Reputation: 1330

Catel MVVM: How to pass data between windows

Hi all I am struggling with the process of sending data between view models in Catel MVVM model. I have a button which on click I want to open a new window and send some data (object) to the newly opened window. However I am unable to solve this by myself so can you help me out?

In my first View Model I have:

private readonly IShowStopInfo stopInfo;

//Main constructor of the class
public StopViewModel(IGrtrService grtrService, IShowStopInfo stopInfo)
{
    this.stopInfo = stopInfo;

    Argument.IsNotNull(() => grtrService);
    _grtrService = grtrService;

    AllStops = _grtrService.LoadStop();
    Stop_Line = _grtrService.LoadLines();

    ShowSelectedValue = new Command(OnStopsInfo);
}
public Command ShowSelectedValue { get; private set; }

private void OnStopsInfo()
{
    stopInfo.ShowStopInfo();
}
//Getting Selected Stop from the list
public Stop SelectedStop
{
    get { return GetValue<Stop>(SelectedStopProperty); }
    set { SetValue(SelectedStopProperty, value); }
}
public static readonly PropertyData SelectedStopProperty =  RegisterProperty("SelectedStop", typeof(Stop));

In my case I want to send the result from the method "SelectedStop" how can I do that?

Upvotes: 0

Views: 842

Answers (1)

Geert van Horrik
Geert van Horrik

Reputation: 5724

Please check out the "Getting started with WPF" section of Catel. It will guide you through creating a first application with all the basics. One of the basics is showing a window with a context.

You can find a very detailed explanation (with screenshots, etc) here.

Basically you must use the first parameter of the window view model as model injection (in the example it's the Person model).

Then check out the "hooking up everything together" part, especially the commands (where the IUIVisualizerService is used to create a window with the currently selected person or family).

For example, the detail vm would look like this:

public class MyStopDetailsViewModel : ViewModelBase
{
    public MyStopDetailsViewModel(Stop myStop, IMessageService messageService, IPleaseWaitService pleaseWaitService)
    {
         // Here is the context with the injected Stop parameter. Note that I have
         // also injected several other services to show how you can use the combination
    }
}

You can call this (as already shown in the example) as follows:

var typeFactory = this.GetTypeFactory();
var stopDetailsViewModel = typeFactory.CreateInstanceWithParametersAndAutoCompletion<MyStopDetailsViewModel>(SelecteedStop);
_uiVisualizerService.ShowDialog(stopDetailsViewModel );

Upvotes: 1

Related Questions