2

I implemented a sort of a wizard. With multiple windows. Each viewmodel of each window is a subclass of one base super class (also a viewmodel). In the first window, i fill up a property, which is in the base class. But in the next window, if i want to get that property, it is null. This is quite annoying and i really don't know how this come.

This is my BaseViewModel:

public abstract class WijzigToetsBaseViewModel : INotifyPropertyChanged
    {
        #region Fields

       Examination exam;           

        #endregion // Fields

        #region Constructor

        protected WijzigToetsBaseViewModel()
        {

        }



        #endregion // Constructor

        #region Properties

        public Examination Examination
        {
            set { if(value != null)
                exam = value; this.OnPropertyChanged("Examination");
            }
            get { return exam; }
        }

So i the "shared" property is examination, in the first window , i fill this up by:

(viewmodel of the first window, implements model above)

 public string Pad
        {
            get { return pad; }
            set { pad = value;
            OnPropertyChanged("Pad");

            this.Examination = XmlConversionExamination.ReadExamination(value);
            Naam = this.Examination.Name;
            }
        }

But then, if i want to get the property "examination" in the second window,it gives null:

 public string Test
        {
            get {

                    return this.Examination.Name;             
            }
            set { test = value;
            OnPropertyChanged("Test");
            }
        }

(this is in second viewmodel, also implements base viewmodel.) so here this.Examination = null. This is kind of strange because i setted Examination property in the first window. Someone who has an idea? Thanks

Ruben
  • 1,033
  • 3
  • 12
  • 22

2 Answers2

1

If you have two different ViewModels, then you thereby have two instances of the "base" ViewModel. So this is expected since Examination is an instance property.

Daniel Rose
  • 17,233
  • 9
  • 65
  • 88
  • So how can i fix this problem? Binding each view to the same viewmodel maybe? – Ruben Apr 26 '11 at 11:34
  • Binding to the same instance of your viewmodel should fix your prpblem – blindmeis Apr 26 '11 at 11:59
  • The question is what "Examination" is supposed to represent. Is it a "singleton" property? In that case you could make it static, for example. Or are there supposed to be multiple independent values of the property, but these two ViewModels should share the value? In that case, look at ViewModel communication, ex. MVVM Mediator pattern. – Daniel Rose Apr 26 '11 at 12:11
0

It sounds to me like you should use the same instance of ViewModel in your different "pages". That ViewModel would contain the properties on both pages but the View would only expose the relevant ones on the page. You might not want to roll your own Wizard, maybe use something like this, or like this post suggests.

Community
  • 1
  • 1
Jose
  • 10,891
  • 19
  • 67
  • 89