How do I set a property in a ViewModel from another ViewModel in MVVM Light Toolkit

2.8k views Asked by At

I have a form containing few TextBox elements and a progress bar. I want the progress bar to be updated when the TextBox have some values assigned.

So when a property is set, i try to increment ProgressPercent, but doesn't actually work. What am I missing, Why can't I access ProgressPercent from another ViewModel?

MainViewModel.cs

public class MainViewModel : ViewModelBase
{
    private int progressPercent { get; set; }

    public MainViewModel()
    {
    }

    public int ProgressPercent
    {
        get
        {
            return this.progressPercent;
        }
        set
        {
            this.progressPercent = value;
            this.RaisePropertyChanged(() => this.ProgressPercent);
        }
    }
}

FooterViewModel

public class FooterViewModel : ViewModelBase
{
    private string firstName { get; set; }
    private string lastName { get; set; }

    public FooterViewModel()
    {
    }

    public string FirstName
    {
        get
        {
            return this.firstName;
        }
        set
        {
            this.firstName = value;
            this.RaisePropertyChanged(() => this.FirstName);
            Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
            {
                ProgressPercent += 10; 
            });
        }
    }

    public string LastName
    {
        get
        {
            return this.lastName;
        }
        set
        {
            this.lastName = value;
            this.RaisePropertyChanged(() => this.LastName);
            Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
            {
                ProgressPercent += 10; 
            });
        }
    }
}

HeaderViewModel.cs

public class HeaderViewModel : ViewModelBase
{
    private string address { get; set; }
    private int age { get; set; }

    public HeaderViewModel()
    {
    }

    public string Address
    {
        get
        {
            return this.address;
        }
        set
        {
            this.address = value;
            this.RaisePropertyChanged(() => this.Address);
            Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
            {
                ProgressPercent += 10; 
            });
        }
    }

    public int Age
    {
        get
        {
            return this.age;
        }
        set
        {
            this.age = value;
            this.RaisePropertyChanged(() => this.Age);
            Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
            {
                ProgressPercent += 10; 
            });
        }
    }
}

The question Accessing a property in one ViewModel from another is not different, but the solution there is not working for me, for some reason

Actually the answer is here Accessing Properties in other ViewModels in MVVM Light

1

There are 1 answers

1
kronkk On BEST ANSWER

You typically have to do some type of communication infrastructure to talk between ViewModels. Are you using any type of Mediator or MVVM Framework that has scaffolding for this built in?

Alternatively you could set a property in a class that all of them have access to and that could raise an event that the other ViewModels are subscribing to.