prism-unity how to passed data to viewmodel class

Topics: Prism v4 - WPF 4
Feb 23, 2013 at 10:28 AM
Hi everyone,

I have question on passed data to viewmodel.

I have controller base abstract class who provides virtual generic method:
        protected virtual void ActivateView<T>(string regionname, string viewname, object viewModelData = null)
        {
            var _view = this.RegionManager.Regions[regionname].GetView(viewname);
            if (_view == null)
            {
                _view = this.Container.Resolve<T>(viewname);
                this.RegionManager.Regions[regionname].Add(_view, viewname);
            }
// Before Activate View in region I must do it something to pass data to viewmodel :(
            this.RegionManager.Regions[regionname].Activate(_view);
        }
It is based class (methods) for all controllers who activates views in regions in my app.
ViewModel to active view is connected via a Dependency:
        public MyView()
        {
            InitializeComponent();
        }

        [Dependency]
        public ViewModel View_Model
        {
            get { return (View_Model)this.DataContext; }
            set { this.DataContext = value; }
        }
It works very fine but problem is when I will passed data to viemodel (who is related with view who is activated in region) via ActivateView<T>(...) method. In list of parameters is something like "object viewModelData" - this is it what I want pass to viewmodel linked with activated view.

I have not idea how do it :( Maybe somebody did have similar problem - please help me :)

Kind Regards,

Kri$

P.S. Sorry form my language still I learn it :)
Feb 25, 2013 at 4:35 PM
Edited Feb 25, 2013 at 4:42 PM
Hi,

Based on the code snippets you provided, I believe that a possible solution for your problem could be, before activating the view, passing the viewModelData parameter to a SetViewModelData method (which will modify your view model data) from your view model like this:
_view.View_Model.SetViewModelData(viewModelData);
This should work since you have a direct reference to your view after you resolve it from the container, so you should be able to access all its classes and methods. If this approach is not possible in your scenario, it would be helpful if you can provide us with more information about how you manage those views and view models.

Please let me know if this helps,

Federico Martinez
http://blogs.southworks.net/fmartinez