Monday, September 27, 2010

Building Silverlight Application using MVVM Part 4 - Delete Item

This is part four of Silverlight Application using MVVM-CRUD Operation that showcases the basic building blocks of a data centric application. 
Deleting an item is quite similar to adding an item. Instead of adding new item to the collection, we just remove the selected item from the collection.

To allow user to delete the selected person, add new Delete button to StackPanel in which Add button was added earlier. The xaml code for that stackpanel will look like as shown below -








Additionally, set the SelectedItem attribute of the DataGrid as
SelectedItem = "{Binding SelectedPerson, Mode=TwoWay}"
in Mainpage.xaml file. The code for datagrid look like as shown below -



















Add a Property "SelectedPerson" and method "DeletePerson" to delete the selected person in the PeopleViewModel.cs file in the ViewModel folder as shown below -


















Now add handler for delete click event in MainPage.xaml.cs as shown below -






F5 and run the application. The UI will be as shown below -














Select any row from the datagrid and click on delete button, corresponding row will be delted from the datagrid.

At this point we have a complete functioning application that allow user to add data, update data and delete. Next we will take on validation. 

5 comments:

  1. Hey this is great code that is very helpful .

    ReplyDelete
  2. hey can u please tell me how to delete user using domain services in mvvm pattern..please it would be of grt help please

    ReplyDelete
  3. hai ......nice article



    i want how to convert existing Silverlgiht 4 project to MVVM in detaild

    and using WCF

    ReplyDelete
    Replies
    1. Go through these links -
      http://www.silverlight.net/learn/advanced-techniques/the-mvvm-pattern/using-the-mvvm-pattern-in-silverlight-applications

      http://www.silverlight.net/learn/advanced-techniques/the-mvvm-pattern/how-do-i-build-data-driven-silverlight-application-using-the-mvvm-pattern

      Delete
  4. Thanks!
    Explained clearly without implementing ICommand interface,
    exactly what i needed

    ReplyDelete