Implementing view-model and binding to grid
hi,
i new mvvm. using mvvm light tool kit.
i trying create simple silverlight application displays products in gridview.
can guide me on how to
- consume wcf service in mvvm light
- what changes should make in viewmodel , viewmodellocator collection of data wcf service.
- how bind data gridview in mainpage.xaml
below code have implimented,
mainviewmodel.cs
using galasoft.mvvmlight; using nyntraonlineshopping.nyntraservicereference; using system.collections.objectmodel; namespace nyntraonlineshopping.viewmodel { public class mainviewmodel : viewmodelbase { readonly nyntraserviceclient _clientcontext = new nyntraserviceclient(); private observablecollection<product> _products = new observablecollection<product>(); public observablecollection<product> products { { return _products; } set { _products = value; } } public mainviewmodel() { _clientcontext.getallproductscompleted += clientcontextgetallproductscompleted; } void clientcontextgetallproductscompleted(object sender, getallproductscompletedeventargs e) { _clientcontext.getallproductsasync(); } } }
viewmodellocator.cs
namespace nyntraonlineshopping.viewmodel { public class viewmodellocator { static viewmodellocator() { servicelocator.setlocatorprovider(() => simpleioc.default); if (viewmodelbase.isindesignmodestatic) { simpleioc.default.register<inyntraservice, nyntraserviceclient>(); } else { simpleioc.default.register<inyntraservice, nyntraserviceclient>(); } simpleioc.default.register<mainviewmodel>(); } [system.diagnostics.codeanalysis.suppressmessage("microsoft.performance", "ca1822:markmembersasstatic", justification = "this non-static member needed data binding purposes.")] public mainviewmodel main { { return servicelocator.current.getinstance<mainviewmodel>(); } } [system.diagnostics.codeanalysis.suppressmessage("microsoft.performance", "ca1822:markmembersasstatic", justification = "this non-static member needed data binding purposes.")] public productviewmodel product { { return servicelocator.current.getinstance<productviewmodel>(); } } public static void cleanup() { } } }
and mainpage.xaml
<usercontrol xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" x:class="nyntraonlineshopping.mainpage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:ignorable="d" height="300" width="300" datacontext="{binding source={staticresource locator}, path=viewmodelname}"> <usercontrol.resources> <resourcedictionary> <resourcedictionary.mergeddictionaries> <resourcedictionary source="skins/mainskin.xaml" /> </resourcedictionary.mergeddictionaries> </resourcedictionary> </usercontrol.resources> <grid x:name="layoutroot"> <telerik:radgridview itemssource="{binding products}" autogeneratecolumns="true" > </telerik:radgridview> </grid> </usercontrol>
i not getting errors. not getting result aswell.
public mainviewmodel() { _clientcontext.getallproductscompleted += clientcontextgetallproductscompleted; _clientcontext.getallproductsasync(); } void clientcontextgetallproductscompleted(object sender, getallproductscompletedeventargs e) { if (e.error == null) { return; } foreach (product item in e.result) { products.add(item); } }
Silverlight > MVVM / ViewModel Pattern with Silverlight
Comments
Post a Comment