I am learning MVVM using sample created by Josh Smith at http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
I wanted to add a functionality of update in the existing code,
Like when user sees data on the Grid of 'All Customers' user can edit particular record by double clicking it, double click will open up new tab (same view/viewmodel that is used for new customer). I don't have any idea how to do it, do i have to call it through mainwindowviewmodel or there is some other way.
Thank You All and Happy Programming
GAurav Joshi
It's a bit involved, so let's take it one thing at a time:
The first thing you need to do is to let the View Model know which item is selected. To do this, you will need to add an IsSelected property to Customer
public bool IsSelected { get; set; }
(Edit: As has been pointed out to me, the CustomerViewModel class already has this property, so the above is not necessary for that particular project - although it is in general.)
You then need to databind the IsSelected property to the items in the ListView. One way to do this is through a style that targets Customer. Something like this:
<Style x:Key="CustomerListStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />
</Style>
Then assign this style using the ItemContainerStyle of the ListView:
<ListView ItemContainerStyle="{StaticResource CustomerListStyle}" ...>
To be able to edit a selected Customer, you should add an EditCostumer command to AllCustomersViewModel. Implement this Command using RelayCommand to show the edit view for the selected item.
You can use LINQ to find the Customer that has IsSelected == true
.