Structure of Program
Currently I am using the MVVMLight 4.1 framework in my application.
I have a view model XViewModel
which wraps around an instance of XClass
, X
. X
contains many properties such as S
. I also have another instance of XClass
in another ViewModel.
ViewModel
public XViewModelClass XViewModel : ViewModelBase
{
public XClass X
{
get
{
return x;
set
}
if(value == x)
{
return;
}
var oldValue = x;
x = value;
RaisePropertyChanged(XPropertyName, oldValue, x, true)
}
}
private XClass x;
public const string XPropertyName = "X"
}
ViewModel2
public YViewModelClass YViewModel : ViewModelBase
{
public YViewModel()
{
Messenger.Default.Register<PropertyChangedMessage<XClass>>(this, message =>
{
X2 = message.NewValue
});
}
public XClass X2
{
get
{
return x2
set
}
if(value == x2)
{
return;
}
var oldValue = x2
x2= value;
RaisePropertyChanged(X2PropertyName)
}
}
private string x2;
public const string XPropertyName = "X2"
}
Model
public class X : ObservableObject
{
public string S
{
get
{
return s;
set
}
if(value == S)
{
return;
}
s = value;
RaisePropertyChanged(SPropertyName)
}
}
private string s;
public const string XPropertyName = "S"
}
Problem
How do I ensure that when any property in X
changes (e.g. S
is set to a different value), RaisePropertyChanged
is called for X
. It would be best if I don't have to send a property changed message for every property in my model.
The reason behind is that I have another instance of XClass
, `X2' in another ViewModel and I want to keep both instances in sync.