0

I've been doing a Wpf program for some time now. But when i create a application and create multiple viewmodel. And i need to access a property in a different view model.

So in order to access it. I need each viewmodel to be static.

Then i searched how does view model should be communicating to each other.

Then i found an answer here

https://stackoverflow.com/questions/4892731/wpf-mvvm-how-do-viewmodels-communicate-with-each-other

So i searched for prism aggregator and i found a tutorial

https://www.c-sharpcorner.com/UploadFile/5ffb84/prism-event-aggregator-in-wpf-with-mvvm/

Then i followed it.

just like the tutorial. I added a event class in the Model folder

And here is the code

namespace MdbMerge.Model
{
   public sealed class Event
    {
        #region Class Properties

        internal static Event EventInstance
        {
            get
            {
                return eventInstance;
            }
        }

        #endregion

        #region Instance Properties

        internal IEventAggregator EventAggregator
        {
            get
            {
                if (eventAggregator == null)
                {
                    eventAggregator = new EventAggregator();
                }

                return eventAggregator;
            }
        }

        #endregion

        #region Constructors

        private Event()
        {

        }

        #endregion

        #region Class Fields

        private static readonly Event eventInstance = new Event();

        #endregion

        #region Instance Fields

        private IEventAggregator eventAggregator;

        #endregion

    }
}

but when i set the datacontext of my viewmodel it gives me error

DataContext = new MdbMergeViewModel(MdbMerge.Model.Event.EventInstance.EventAggregator);

It says. MdbMerge does not contain a definition for model.

So how can i solve this?

Why does the Methods inside is not read?

enter image description here

Here is the screenshot of the structure of my program

enter image description here

Thank you.

Ramon bihon
  • 385
  • 1
  • 5
  • 18
  • `i need to access a property in a different view model` - then you're doing it the wrong way. View models provide data to views not other view models. – Haukinger Feb 19 '19 at 07:24
  • @Haukinger i need to access a property. in order for this view model to know if it a command can be executed. Am i doing it wrong? thank you – Ramon bihon Feb 19 '19 at 08:34
  • As @Haukinger said, this doesn't seem the correct way to go. A ViewModel should only present data from a Model to the View. If the ViewModel requires additional information then it should probably have a reference to the Model where that information is stored. ViewModels should not be static either. I think the issue you are experiencing though is you have a Namespace and Class clash(s) (Model and MdbMerge) - (Edit multiple clashes) – Martin Grundy Feb 19 '19 at 08:42
  • Can you please link me to a good mvvm tutorial? Thank you. So meaning i've been doing it the wrong way – Ramon bihon Feb 19 '19 at 23:55

0 Answers0