0

Yesterday i ask here how does view model communicate it each other. Because the design of my program is. A viewmodel contains a property. Example a property named DirectoryPath and in the other View model i want to access the value of the DirectoryPath

And below is the link

Prism Event Aggregator - method cannot be read

Then another problem comes in. The comment says. I'm doing it wrong.

Let's say I have a textbox.

Then i want to create a DirectoryPath string property which will be bind in the text property. Then where should i put this property?

And how can i access this? In my viewmodel I have a command. How can i set the text of the DirectoryPath when a user click a button?

All this month i spent doing MVVM and WPF is wrong? And i want to fix this.

Sample code of my ViewModel

class ViewModel : INotifyPropertyChanged
    {
        private string _DirectoryPath;

        public string DirectoryPath
        {
            get { return _DirectoryPath; }
            set
            {
                _DirectoryPath = value;
                OnPropertyChanged("DirectoryPath");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string name)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
    }
Ramon bihon
  • 385
  • 1
  • 5
  • 18
  • Seems perfectly fine. The view knows about the viewmodel, so the view can set `DirectoryPath`. You need a way to translate the ViewModel into a Model and vice versa, but that's trivial. – MineR Feb 20 '19 at 02:10
  • @MineR Can you please suggest or teach me how to make the view model communicate with each other – Ramon bihon Feb 20 '19 at 02:25
  • You'll need to ask a single specific question with code that illustrates what you are trying to do, and what specifically isn't working. – MineR Feb 20 '19 at 02:30

1 Answers1

1

Then i want to create a DirectoryPath string property which will be bind in the text property. Then where should i put this property?

The place of you property looks right to me.

In my viewmodel I have a command. How can i set the text of the DirectoryPath when a user click a button?

You add a delegate to the command, within the delegate you will set the DirectoryPath variable to the value you want.

  public Command Something => new Command (x => DirectoryPath = 'this is you value');
David
  • 505
  • 3
  • 8