Nikhil Vaidya
Nikhil Vaidya

Reputation: 93

How to communicate with ViewModel from Model

I am quit new to MVVM. So please correct me if I am doing any mistake in implementing MVVM.

In my Model class there are two properties Price and IsChecked.

    public int Price { get; set; }
    public static int _total;

    bool _isChecked;
    public bool IsChecked
    {

        get
        {
            return _isChecked;

        }
        set
        {
            _isChecked = value;

            if (value == true)
            {
                _total+= this.Price;
            }
            else
            {
                _total-= this.Price;
            }


        }
    }

In My ViewModel Class there is a property of Type List <Model > and it is bounded to datagrid in view and another property is Total, which bounded to a textBlock in View.

    public int Total
    {
        get
        {
            return  DocumentStoreModel._total;

        }
        set
        {

        }

    }

DataGrid has a checkBox column and it is bounded with Ischecked property

<DG:DataGridCheckBoxColumn Header="Select" Binding="{Binding IsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ></DG:DataGridCheckBoxColumn>

Now, if user checked the Checkbox from checkbox column in DataGrid then total should be reflected in View.

My ViewModel class is implementing the INotifyPropertyChanged interface.

My qus is,if my model's property is changing how i can tell it to my viewModel ?

please let me know How i can achieve this.

Upvotes: 3

Views: 1005

Answers (3)

Dean Chalk
Dean Chalk

Reputation: 20451

As you are exposing your Model within your ViewModel, then you need to implement INotifyPropertyChanged in your Model. You however have a problem in that your Total property is static and (afaik) you cant use INotifyPropertyChanged for static properties. I would suggest you create a custom event on your model that can be subscribed to on your ViewModel. Here's an example (you might want to tidy it up a bit).

public class Model : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    internal static event EventHandler TotalChanged;
    internal static int Total { get; private set;}

    private int price;
    public int Price
    {
        get { return price; }
        set
        {
            price = value;
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("Price"));
        }
    }

    private bool isChecked;
    public bool IsChecked
    {
        get { return isChecked; }
        set
        {  isChecked = value; 
            if (value)
                Total += Price;
            else
                Total -= Price;
            if (TotalChanged != null)
                TotalChanged(this, EventArgs.Empty);
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("IsChecked"));
        }
    }
}

public class MainViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public int Total
    {
        get { return Model.Total;  }
    }

    public MainViewModel()
    {
        Model.TotalChanged += TotalChanged;
    }

    private void TotalChanged(object sender, EventArgs e)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs("Total"));
    }
}

Upvotes: 0

Jon Mitchell
Jon Mitchell

Reputation: 3419

From what I can tell your ViewModel's Total property is pointing to your Model's Total property so any changes you make to the Model's total will essentially be changed for the ViewModel as well.

What you might have to do though is raise the PropertyChanged event for the Total property on the ViewModel when IsChecked is changed. This will tell your View to update the data for your total text block.

Upvotes: 2

Aliostad
Aliostad

Reputation: 81660

You need to implement a Command (by implementing ICommand or variants of it) and expose as property on the ViewModel and bind it on the view for the event on the checkbox.

Upvotes: 0

Related Questions