-1

I am developing a UWP application where i am following MVVM pattern.

I have a property in the View Model which is bind to the view. I have one function in the service which process multiple tasks.

After each execution of activity i need to update the property which is in the View Model.

ViewModel.cs

 public Brush CurrentGetExecutionColor
        {
            get { return _currentGetExecutionColor; }
            set { Set(ref _currentGetExecutionColor, value); }
        }

public DelegateCommand DelegateCommandProcess
            => _delegateCommandProcess ?? (_delegateCommandProcess = new DelegateCommand(async () =>
            {
                await _service.ProcessMethod();
            }));

Service.cs

    private async Task<bool> ProcessMethod()
    {
       While(condition)
       {
          Process();
          //UpdateViewModel property
         CurrentGetExecutionColor = Color.Red;
       }
    }

How i can achieve this functionality so that i can update View Model property from service.

Thanks in Advance.

  • 1
    You are looking for this: http://stackoverflow.com/questions/15439841/mvvm-in-wpf-how-to-alert-viewmodel-of-changes-in-model-or-should-i – Alex Dec 13 '16 at 10:19
  • 1
    You could implement INotifyPropChanged on the Model , and let the VM subscribe to that. But it depends on lots o details, the question isn't very complete. – H H Dec 13 '16 at 10:19
  • i have updated my post. can you please take a look. Thanks :) – Bhushan Panhale Dec 13 '16 at 13:38

1 Answers1

0

Try to implement in your property OnPropertyChanged, like this:

private Type _yourProperty;
public Type YourProperty
{
   get { return _yourProperty; }
   set
   {
      _yourProperty = value;
      OnPropertyChanged();
   }
}


public event PropertyChangedEventHandler PropertyChanged;    
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
   PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
M. Pipal
  • 734
  • 11
  • 24
  • What's the `handler = ...` for? If you use null propagation, that's completely superfluous, just call `PropertyChanged?.Invoke...` directly. – Haukinger Dec 13 '16 at 15:33