data binding - WPF DataGrid: How do I databind the properties of the SelectedItem to trigger INotifyPropertyChangedEvents? -
i'm trying mvvm possible:
 model (interesttypeentity) implements inotifypropertychanged.
 viewmodel (interesttypeallviewmodel) has observablecollection binds datagrid. when changes made it, sends changes (add/remove) database.
the problem is, want able update database when properties of objects within collection change. i'm not sure how that? here's code far...
xaml:
<datagrid name="testgrid" grid.row="3" grid.columnspan="2" autogeneratecolumns="false"           itemssource="{binding inttypes}" selecteditem="{binding currentinttype}">     <datagrid.columns>         <datagridtextcolumn header="interest id" binding="{binding inttype}" />         <datagridtextcolumn header="interested parties description" binding="{binding description}" maxwidth="500" />     </datagrid.columns> </datagrid>   viewmodel code:
public observablecollection<interesttypeentity> inttypes  {     { return datarepository.interesttypeentities; } }  public interesttypeentity currentinttype { get; set; }  public int16 inttype {     { return currentinttype.inttype; }     set     {         if (value != currentinttype.inttype)         {             currentinttype.inttype = value;             onpropertychanged("inttype");         }     } }  public string description {     { return currentinttype.description; }     set     {         if (value != currentinttype.description)         {             currentinttype.description = value;             onpropertychanged("description");         }     } }      
don't create collection of model objects, , don't implement inttype , description properties on (current) view model.  , unless have other reason so, don't implement property-change notification in model.
instead, make inttypes collection of interesttypeentityviewmodel objects.
this class wraps interesttypeentity.  exposes inttype , description properties a) wrap underlying interesttypeentity properties , b) performs property change notification.  if make constructor take interesttypeentity argument, it's easy populate in view model:
inttypes = new observablecollection<interesttypeentityviewmodel>(    datarepository.interesttypeentities.select(x => new interesttypeentityviewmodel(x));   bind itemssource collection.  (also, make currentinttype property of type interesttypeentityviewmodel , raise propertychanged when changes.)
edit:
if owning view model needs notified when properties change on items in collection, it's pretty simple make handle propertychanged events they're raising.  in constructor, add:
foreach (interesttypeentityviewmodel vm in inttypes) {   vm.propertychanged += interesttypeentityviewmodel_propertychanged; }   and method:
private void interesttypeentityviewmodel_propertychanged(object sender, propertychangedeventargs e) {    interesttypeentityviewmodel vm = (interesttypeentityviewmodel) sender;    // check e.propertyname , whatever need here. }   don't forget unregister event handler if remove object collection; otherwise, child view model objects won't disposed until parent 1 does.
note, way, implementing view models way, can exercise lot of control on updates underlying entity model. instance, can implement command in parent view model of updates in single operation, , 1 lets user discard of changes they've made in ui without performing update. , of logic nicely decoupled actual presentation layer.
Comments
Post a Comment