Hey.
I'm building my first app using MVVM and I stuck. Using the messenger to communicate other ViewModels about some changes was working ok until I tried to build an observablecollection of objects. I have a class (myClass) that contains a list (innerList). Elements of innerLists should send message to it's parent (to communicate that they changed or should be deleted). I'm sending a message from the innerList object. This message is registered in the myClass.
The problem is that I need to create ObservableCollection of myClass objects. As I understand the idea of sending - receiving messages, every instance of myClass will receive the message sent from the element of innerList. The goal is to be able to receive the message only in the parent myClass object, and not every object of myClass.
The structure I described is much more complicated so that's just the sketch of the situation. The question - is it possible to send a message only to parent object using Messenger's Send Register functionality. Maybe it should be solved in some other way?
Below is how the two classes are built. I shortened it to only this what's important here.
public class ObjTypeListVM : ViewModelBase
{
private ObservableCollection<ObjTypeVM> objTypeList = new ObservableCollection<ObjTypeVM>();
public ObjTypeListVM()
{
Messenger.Default.Register<Messages.ObjTypeModifiedMsg>(this, ObjTypeModified);
}
public ObservableCollection<ObjTypeVM> ObjTypeList
{
get { return objTypeList; }
}
public void ObjTypeModified(Messages.ObjTypeModifiedMsg msg)
{
switch (msg.Type)
{...
case "delete":
Delete(msg.ObjType);
break;
default:
break;
}
}
public void Delete(ObjTypeVM objType)
{...
ObjTypeList.Remove(objType);
}
}
public class ObjTypeVM : ViewModelBase
{
private XmlSTSLib.ObjType objType;
int objTypeId;
private RelayCommand deleteItemCmd;
public ObjTypeVM(ObjType _objType, int _objTypeId)
{ ... }
public int ObjTypeId
{
get { return this.objTypeId; }
set { this.objTypeId = value; RaisePropertyChanged("ObjTypeId"); }
}
public RelayCommand DeleteItemCmd
{
get
{
if (deleteItemCmd == null)
{
deleteItemCmd = new RelayCommand(Delete);
}
return deleteItemCmd;
}
}
public void Delete()
{
Messenger.Default.Send<Messages.ObjTypeModifiedMsg>(new Messages.ObjTypeModifiedMsg(this, "delete"));
}
}