views:

95

answers:

3

So, this is a pretty basic question I hope.

I have a web service that I've added through Add Service Reference. It has some methods to get list and get detail of a perticular table in my database.

What I'm trying to do is setup a UI as follows:

  1. App Load
    1. Load service proxy
    2. Call the GetList(); method display the results in a ListBox control
  2. User Double Clicks item in ListBox, display a modal dialog with a "detail" view

I'm extremely new to using MVVM, so any help would be greatly appreciated.

Additional information:

// Service Interface (simplification):

interface IService 
{
    IEnumerable<MyObject> GetList();
    MyObject GetDetail(int id);
}

// Data object (simplification)
class MyObject
{
    public int ID { get; set; }
    public string Name { get; set; }
}

I'm thinking I should have something like this:

MainWindow
    MyObjectViewUserControl 
        Displays list
        Opens modal window on double click

Specific Questions:

  1. What would my ViewModel class look like?
  2. Where does the code to handle the double click go?
    1. Inside the UserControl?
    2. What about Commands, would this be a good application of them?

Sorry for the long details, but I'm very new to the whole thing and I'm not educated enough to ask the right questions.

I checked out the MVVM Sample from wpf.codeplex.com and something isn't quite clicking for me yet, because it seems very confusing.

+3  A: 
  1. Your VM would have:
    • an ObservableCollection that contains items that you bind to your ListBox.
    • CurrentItem that is bound to a currently selected item in the list
    • DelegateCommand that is bound to a double click and that invokes a logic to show a detail view (see MVVM ways of doing this, either a service or event aggregator like approach)
    • A logic to invoke invoke GetList() and then populate this colleciton
  2. The code to handle double click will go to VM (see above)
PL
+1  A: 

You will have 2 ViewModels because you have 2 views

1 ViewModel will have

  1. Your service proxy
  2. List
  3. ShowDetailFor(string selectedItemFromList) This should create a new instance of VM2 passing it the service proxy in ctor and invoke ShowDetail on VM2.

2 ViewModel will have

  1. MyObject (if it is just messagebox, no need to have ViewModel)
  2. ShowDetail(int id)

Once you have the ViewModel use WPF binding and command to connect data from VM to XAML

MVVM for user control becomes a bit more complicated, so if possible stay away from UserControls for now.

Nitin Chaudhari
A: 

See the RegularExpressionViewModel example in my yodelay project.

The majority of your code will go in your ViewModel. The only code that should be in your view is code that wires the events of the View to the ViewModel. Usually no code will be required in the View since most of the wiring can be handled by DataBinding.

I use a presentation abstraction layer with Dependency Injection to communicate with dialogs.

Chris McKenzie