1

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

Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
Gaurav
  • 23
  • 3

1 Answers1

1

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.

Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
  • Hi Mark, Thank You for your post and Sorry I am writing in comments, didn't found any other way on stackoverflow to reply. Do you think it is ok to declare 'workspace' static and creating new command in customerviewmodel it self which will create new workspace and add it to the mainwindowviewmdodel workspace. that way idon't have to implement isselected. Just want some expert inputs Thank You Gj – Gaurav Jun 29 '09 at 17:06
  • In general, I try to stay away from statics as much as possible, since it tends to hurt Testability, flexibility and just general OO-ness. The whole point of ViewModels is that they model Views. You can't have one Model per View if there are no instances of the Model. It may be the case that there is only one Workspace anyway, but just the same I'd say that using statics is to go against the whole philosophy behind MVVM. I know my reasoning sounds a bit vague, but those are just general considerations based on years of experience :) – Mark Seemann Jun 29 '09 at 17:19
  • 1
    Mark. The only Issue I see in your reply is that you recommend putting the IsSelected property in the Customer Model. I think it should go in the CustomerViewModel class because it is a GUI oriented property, and a Customer Model object really has no concept of what being selected means. – Tim Rupe Jun 29 '09 at 17:32
  • 1
    I just checked again, and that project does already have a IsSelected property for the CustomerViewModel class. – Tim Rupe Jun 29 '09 at 17:34
  • @Tim Rupe: You are absolutely right - my mistake. I'll edit my answer based on your observation. Thank you. – Mark Seemann Jun 29 '09 at 18:54
  • Mark: how would you create the new workspace for the item edited? – Eduardo Molteni Oct 16 '09 at 19:10
  • @Eduardo Molteni: Take a look a this SO answer: http://stackoverflow.com/questions/1023995/mvvm-and-commands-that-show-more-gui/1024011#1024011 – Mark Seemann Oct 16 '09 at 22:09