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.
As you are exposing your Model within your
ViewModel, then you need to implementINotifyPropertyChangedin your Model. You however have a problem in that your Total property is static and (afaik) you cant useINotifyPropertyChangedfor 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).