michaelscodingspot / WPF_MVVMC

A WPF framework for navigation between pages with MVC-like pattern
MIT License
64 stars 18 forks source link

How to pass dynamic data to WizardController ? #18

Closed acrolink closed 10 hours ago

acrolink commented 5 days ago

e.g. when opening the MainWindow holding it?

acrolink commented 4 days ago

I have solved the issue by adding a DATA property to the controller, see the commit at my forked repo. Maybe should be considered in future release?

https://github.com/acrolink/WPF_MVVMC/commit/af18752af956c83b3d01bf86a866a5e712860894

michaelscodingspot commented 1 day ago

Hi, You can get the controller instance and set whatever properties you want to the object. i.e.

var controller = _navigationService.GetController<MyController>();
controller.MyDataProperty = data;

If you want to do something when a controller is created, you can subscribe to the ControllerCreated event

MVVMCNavigationService.ControllerCreated += (controllerId)=> {
    var controller = MVVMCNavigationService.GetController(controllerId);
    if (controller is MyController)
    {
        controller.MyDataProperty = data;
    }
}

Does that help with your use case?

michaelscodingspot commented 1 day ago

Another suggestion: When you're in the MainWindow code-behind, you have the Region control that's tied to the controller. You can get the controllerId directly from the region and set whatever data you need.

var region = MyRegion; // assuming the region's x:name in XAML is "MyRegion"
var controller = _navigationService.GetController(region.ControllerID) as My Controller;
controller.MyDataProperty = data;
acrolink commented 1 day ago

@michaelscodingspot

I have tried this:

var controller = _navigationService.GetController<WizardController>();
controller.MyDataProperty = data;

But getting this error message:

Sequence contains no matching element

acrolink commented 1 day ago

@michaelscodingspot

I have tried this:

var controller = _navigationService.GetController<WizardController>();
controller.MyDataProperty = data;

But getting this error message:

Sequence contains no matching element

This works indeed, only if done inside the ContentRendered event.

michaelscodingspot commented 10 hours ago

Great! I'm closing this issue. Feel free to reopen this one or a new one if you're still having trouble.