0

I have a WPF application that I am trying to keep as close to MVVM as possible. I have a class TitlesModel : MappedViewModelCollection<TitleEditModel, Title>, where MappedViewModelCollection, which has a property public ObservableCollection<TViewModel> Items { get; set; }, which is bound to a DevExpress GridControl. Each row in the control has an Edit and Delete button. When this button is pressed, I would like to bind the TitleEditModel for that row in the grid to a view that is a popup, modal window.

Do I 'tell' the main view model this, and let it instantiate, bind, and show a popup, or merely instantiate a popup, pass the row's EditViewModel to it and let it do it's own thing?

What is the conventional pattern for grid/detail view scenarios like this?

ProfK
  • 49,207
  • 121
  • 399
  • 775

1 Answers1

0

This question recently came up in our company, and it's one where the lines of MVVM become very blurry. I haven't come across a clear statement yet.

Of the options you mentioned, the second one seems more MVVM, but your ViewModel becomes UI aware, which I'd avoid if I were you. Always try to keep up a unidirectional awareness chain (View knows ViewModel knows Model)

To solve this, we introduced Interaction interfaces and had instances generated via Dependecy Injection.This keeps the ViewModels 100% testable, because you can replace the editor with plain code in test scenarios.

Example:

public interface IEditor
{ 
    void Edit(object input);
}

public class EditorView : IEditor
{
    public void Edit(object input)
    {
        // open modal window, set DataContext
    }
}

public class EditorStub : IEditor
{
    public void Edit(object input)
    {
        // alter the properties of input that you want to simulate user interaction on
    }
}
Sebastian Edelmeier
  • 4,095
  • 3
  • 39
  • 60