Edit selected item in modal window

Topics: Prism v4 - WPF 4
Oct 29, 2010 at 3:24 PM
Edited Oct 29, 2010 at 3:25 PM


I'm new with Prism and MVVM, and I can't understand one conceptual thing.

For example, there's an application for managing orders. It can create order, edit order header (date, customer, etc), delete order, edit order contents (add position, edit position, delete position). I want to edit an order position in separate modal window. That is, when user press the button "edit" in the order's view, application must open a new modal window, put into this window new instance of the position's view, and initialize data context of that view with selected order position.

I'm using WPF 4, Prism 4 and MEF. So, as far as I understood, my application should consist of:

  • bootstrapper;
  • shell (contains one region, which order view placed into);
  • order view (user control);
  • order view model;
  • order position view (user control);
  • order position view model.

The problem.

I created a command in order view model and bound it to "edit button". I know, which position is selected, so I have proper order position's view model.

Now, I need to create an instance of order position view, place it into pop up region (this will create a new modal window), and assign existing position's view model to the data context of view. What is the best practice for this? Who should create view? Who should assign view model to data context?

At this moment, I'm thinking about such scenario: order view model publish event, payload is view model of selected position. But who subscribes this event and how it creates view? If instance of view obtained via MEF, how to initialize data context with existing instance of view model? Just set "DataContext" property or somehow else?


Thank you for response.

Oct 29, 2010 at 6:39 PM

take a look here: http://compositewpf.codeplex.com/Thread/View.aspx?ThreadId=231798

Nov 1, 2010 at 1:27 PM

Unfortunately, it's a discussion about "how to create a popup".

Most important question for me - how to assign existing instance of order position's view model. Sample code from that thread:

    public partial class DemoView : UserControl
        //assign the DataContext to the DemoViewModel
        private DemoViewModel ViewModal { set { this.DataContext = value; } }

That is, it creates DemoView instance and DemoViewModels using MEF. So, MEF will use either shared view model instance, or new view model instance. And what about existing instance? Something like this?

 public void ShowModalWindow()
            var view = ServiceLocator.Current.GetInstance<IDemoView>();
            view.DataContext = OrderPostitions.Current;
            regionManager.Regions["SecondaryRegion"].Add(view, "DemoAppView");
            regionManager.RequestNavigate("SecondaryRegion", new Uri("/DemoView", UriKind.Relative));

Nov 1, 2010 at 2:47 PM

basically the way I am doing it at present which may change cause I tend to see an some issue with initial modal edit view creation being blank..  I create the viewmodel as you have with an import and assign the datacontext, followed by all the binding in the xaml markup.  I actually have a property in my viewmodel that is assigned thru an IEventAggregator this isn't the best way since I am using something is more for intra-module communciation and I should probably just use a regular even for inter-module communication.  Anyway I found some behavior example about how to double-click the datarow of the datagrid that allows me to display the modal dialog, this in-turn calls the behavior code for the modal dialog stuff that we are discussing in the thread mentioned

It works but its not perfect yet, of course the question that begs is it correctly following the guidelines for mvvm, at this point I haven't cared, I am sure as I start writing test cases it will eventually matter.