0

I am very new to WPF. I am trying to create Add/Edit form. I have also added Caliburn.Micro frameowrk for MVVM.

I have created one screen for Add or Edit. I do not know how to pass model Id to my ViewModel. How?

Currently my View (Window) works only for Add and not Edit.

  • AddOrderViewModel.cs
  • corresponding AddOrderView.xaml

The classes are like this:

 public class OrderAddEditViewModel : Screen {
        public OrderAddEditViewModel( ) {};

        // I do not know if this is normal approach or not
        public OrderAddEditViewModel(int orderId) { // Load Order from DB};
        ...
   }

public partial class OrderAddEditView : Window ...

Now, when pressing button on Main form, I am opening OrderAddEditView

       //This loads empty form for Add
       // What if I have OrderId and want to load it, HOW?
        var frm = new OrderAddEditView(); 
        frm.Show();

There is some magic behind, and OrderAddEditView knows which view model to use (this is WPF + Caliburn.Micro).

However, currently I want to pass "OrderId" to my OrderAddEditViewModel's constructor (this will load data from DB) and open OrderAddEditView so that constructor with id would be invoked.

renathy
  • 5,125
  • 20
  • 85
  • 149

2 Answers2

1

Since you are using Caliburn.Micro, there is a neat method who opens your ViewModel. Its called ActivateItem. To use it just write the following.

ActivateItem(new YourViewModel([some attributes]));

Update 2: I almost forgot... To use ActivateItem method, one of your ViewModels has to be the Conductor. I like to think about it like base of all views. That's mostly the first ViewModel who starts with your app (except login screen, if you have it). read about it more here.

sharp
  • 1,191
  • 14
  • 39
-2

This is a very uggly solution:

You create a Constructor for OrderAddEditView class.

And pass the id for the DataContext in that constructor?

The ViewModel is the DataContext of the View...

János Tigyi
  • 397
  • 1
  • 9
  • 20
  • 1
    Please, answer a question. If it is ugly suggest something, instead of just saying it is ugly. It is not how stackoverflow works. As I said I am new to WPF. Btw, I do not have construtor yet. This was myu proposal, but if it is ugly, suggest something. I need Add_Edit form (one form for Add and Edit), thus I thought I have to use the same ViewModel - for Add it would be "empty", but for Edit, it would contain data). Moreover, my data access layer is outside WPF, it is as service, but to construct ViewModel I something have to call multiple services. – renathy Jan 28 '18 at 21:26
  • Well, than you should read about MVVM and you should navigate by ViewModel and not by Views. Much easier. For multiple states of View you can use VisualStates. (For example you create a service that makes a new instance of the view if you call that service and it adds your ViewModel instance as a DataContext. Than you just need to bind your new view to a ContentControl content.) – János Tigyi Jan 29 '18 at 10:21