I have question how to raise events in viemodel on instance of model object ?
If I declare the variables directly in viemodel it is easy. just :
private bool _something;
public bool something
{
get
{
return _something;
}
set
{
_something = value;
//do something
RaisePropertyChanged("something");
}
}
but what if in viemodel I have :
private MyModelClass _projekcik;
public MyModelClass Projekcik
{
get
{
return _projekcik;
}
set
{
_projekcik = value;
RaisePropertyChanged("Projekcik");
}
}
where MyModelClass is definied in separate file as :
public class MyModelClass
{
int abc {get;set;}
int other {get;set;}
}
and I want raise an event (execute some part of code from viemodel ) when I change value of Projekcik.abc ?
For example Projekcik.abc and Projekcik.other are variables stored selectedvalues from two comboboxes. And I want raise event to refresh/reload items of second combbbox when user change selected object in first combbbox (when Projekcik.abc are changed)
Your best bet is to implement INotifyPropertyChanged
in the MyModelClass
, and then subscribe to PropertyChanged
event to do your logic. E.g.:
private MyModelClass _Projekcik;
public MyModelClass Projekcik
{
get => _Projekcik;
set
{
if(Equals(value, _Projekcik)) return;
if(_Projekcik != null)
_Projekcik.PropertyChanged -= HandlePropertyChanged;
_Projekcik = value;
if(_Projekcik != null)
_Projekcik.PropertyChanged += HandlePropertyChanged;
void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "abc")
{
//Do your stuff here
}
}
}
}
I assume you know how to properly implement INotifyPropertyChanged
.