I am looking to update the database when an item bound to my View has changed. I noticed that the PropertyChanged event fires in the linq class but how do I tell my viewmodel that something has changed? I am using the Linq class as my model so I don't have to create it all over again, is this bad practice? I know that I could create a new property in my Linq class true or false and use that property from my viewmodel but that wouldn't be too efficent since I would have to redo that each time I needed to update the class from SQL.
Asked
Active
Viewed 276 times
2 Answers
0
- Separation between View and Model is one of the key resposibilities of ViewModel. Else the Model will be polluted with view specific properies and data formattings.
- ViewModel should wrap Model as properties and expose them to the View and as the wrapped ViewModel properies are modified by the View, it can in turn call the model specific methods.
Hence don't bind your linq classes directly to your view, rather expose them through properties in ViewModel. As far as individual collection item notification is concerned here is something that you can use.
-
Ummm... What? How does this answer the question asked? – Ken White Jul 08 '11 at 20:55
-
Right, I understand somewhat the mvvm concept and I am not sure if others generally use a generated linq class as the model but I assume others would. If you didn't then you would have to build an almost identical class. So again what would be the best way to inform my viewModel that a property has been changed in one of my collections? – Jonah Kunz Jul 08 '11 at 21:02
-
Are you having a model collection that needs to be bound to view, then you should expose an ObservableCollection
property in your viewmodel, this will notify only add/remove though. If you are intersted in properites of each item, you should read more into this http://stackoverflow.com/questions/269073/observablecollection-that-also-monitors-changes-on-the-elements-in-collection – anivas Jul 08 '11 at 21:35
0
Just to let anyone else know what I did for this scenario. I did try the link that anivas posted and it worked but wasn't as easy as what I came up with for my solution. I bound the selected item to a property on my modelView since it was the only one that could be changed by the user anyhow. On the setter of my property I put in a handler for the notifyProperty changed. See code below private CustAccountLocation _selectedStore;
public CustAccountLocation SelectedStore
{
get { return _selectedStore; }
set {
_selectedStore = value;
SelectedStore.PropertyChanged += new PropertyChangedEventHandler(SelectedStore_PropertyChanged);
NotifyPropertyChanged("SelectedStore");
}
}
void SelectedStore_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
StoreNeedsSave = true;
}

Jonah Kunz
- 670
- 8
- 19