entity framework - WPF MVVM Light DataGrid and separate Add Item View -
i learning wpf , mvvm , trying create programme has datagrid , button opens form using command can add together item datagrid.
the problem not sure how should implement viewmodels. have 1 viewmodel datagrid form works correctly , using repository retrieve info entity framework.
is possible add together object add together form , have appear in datagrid in other form automatically when press add together button or need refresh action on datagrid? using observable collections in viewmodel , have implemented onpropertychange functionality in collection parameter in view model.
as far can understand when set in repository, every view model gets info should refreshed... though not sure if should write message code work.
i pretty confused , hope can shed lite here... in advance. tell me if have missed mention , add together it:)
edit: how register models:
simpleioc.default.register<parentslistviewmodel>(); simpleioc.default.register<editparentviewmodel>();
and how register repository:
simpleioc.default.register<iparentsrepository, parentsrepository>();
and how retrieve instances of viewmodels:
parentslistviewmodel parentslistviewmodelinstance = servicelocator.current.getinstance<parentslistviewmodel>(); editparentviewmodel editparentviewmodelinstance = servicelocator.current.getinstance<editparentviewmodel>();
the parentslistviewmodel datagrid viewmodel , editparentviewmodel 1 utilize adding/editing records in repository. service locator passes instances of repositories automaticaly have no thought how passes instance of repository viewmodels. pass same instance?
the view models wont automatically refresh when add together repository.
all observable collection inform ui when new item added / removed observable collection (roughly speaking). inotifypropertychange inform ui specific property has changed.
you have few options getting want work (if understand correctly):
you refresh entire observable collection when item added you repository inform datagrid view model event when new item added - datagrid view model can update observable collection you add together form view model talk info grid view model , tell when new item added - in situation you'll need mechanism marshaling info 1 view model another.does help?
edit 12/02/2013 17:30 gmt:
here's quick , dirty illustration of alternative 2 like. requires same instance of repository shared between 2 view models - in case have injected on constructors.
public interface iparentsrepository{ event eventhandler<myitemaddedeventargs> itemadded; //your normal interface implementation here } public class parentsrepository : iparentsrepository { public event eventhandler<myitemaddedeventargs> itemadded; public list<myitem> getallitems() { //logic returns items here homecoming new list<myitem>(); } public void additem(myitem item) { //logic adds item here //fire item added event onitemadded(item); } private void onitemadded(myitem item) { if(itemadded != null) itemadded(this, new myitemaddedeventargs(item)); } } public class myitemaddedeventargs : eventargs { public myitemaddedeventargs(myitem itemadded) { } public myitem itemadded { get; set; } } public class myitem { public string someproperty { get; set; } } public class mydatagridviewmodel { private readonly iparentsrepository _parentsrepository; public mydatagridviewmodel(iparentsrepository parentsrepository) { _parentsrepository = parentsrepository; _parentsrepository.itemadded += _parentsrepository_itemadded; var myitems = _parentsrepository.getallitems(); myitems = new observablecollection<myitem>(myitems); } void _parentsrepository_itemadded(object sender, myitemaddedeventargs e) { if(!myitems.contains(e.itemadded)) myitems.add(e.itemadded); } public observablecollection<myitem> myitems { get; set; } } public class myadditemviewmodel { private readonly iparentsrepository _parentsrepository; public myadditemviewmodel(iparentsrepository parentsrepository) { _parentsrepository = parentsrepository; } //your logic add together item here }
this method adjusted method 1, when _parentsrepository_itemadded event fires, rather adding new item, fetch whole dataset again.
wpf entity-framework mvvm datagrid mvvm-light
No comments:
Post a Comment