This is a mirror of official site:

Get SelectedItems From DataGrid Using MVVM In Silverlight

| Tuesday, July 12, 2011
In this post what I want to learn and to share something like how to get the selected items from the data grid control of the Silver light using MVVM. You can see the main output of the example code in the Image 1, here you can see that I have a data grid control and three button with label Add, edit and delete records. Let me explain you what is happening in the example code, user is allowed to select multiple record from the grid and the selected records Ids are displayed in the "Selected Ids" area. If user select only one record then add and edit buttons are enabled and if records are more then one then add and edit buttons are disabled. Similarly If number of selected item count is shown in the delete button text.


Let us start with the detail of the View Model and the View so that you can have clear understanding of what is happening in the example code.

View Model :
               First I will discuss the important area of the view model,  Here you can see that OnSelectionChanged command takes the object type parameter which is first converted to the IList type in the first statement. Then in the next statement I have set empty string to the SelectedItemIds property which is of type string to display the SelectedItems Ids only below the buttons in the View. In the next statement I have place foreach loop too loop through the selected item of the Data Grid. As the Item Source of the data grid items are of type Customer type. So I have used same type in the foreach loop and then add the ids in the SelectedItemIds and place comma (,) sign between the ids if user has selected more then one item.

Read more: Asim Sajjad
QR: get-selecteditems-from-datagrid-using.html

Posted via email from Jasper-net