0

I want to show a window to create an object based on a form my users fill.

What I do now is from my main view model, I instanciate my form window, set its datacontext to a new instance of the corresponding viewmodel class and get ShowDialog()

        var addLicWnd = new AddLicenseWindow();
        if (addLicWnd.ShowDialog() ?? false)
        {
            var x = ((AddLicenseWindowViewModel)addLicWnd.DataContext).dialogResult;
            //dialogResult is a property I set in my VM to get the data back when the form closes
        }

This ViewModel exposes a dialogResult property to get the data back from the form to my main ViewModel where I will then add it in DB etc ...

My problem here is I cannot close the form within it from a button press for example.

What I found is an answer from a similar question ( https://stackoverflow.com/a/3329467/9734355 ) But the only way I found to use it is to call this SetDialogResult method which takes the window as a parameter. Which means I need to have a reference to my View inside my ViewModel, hence breaking MVVM (or did I miss something?)

So it would look like

Main ViewModel

        var addLicWnd = new AddLicenseWindow();
        addLicWnd.Datacontext = new AddLicenseWindowViewModel(addLicWnd);
        if (addLicWnd.ShowDialog() ?? false)
        {
            var x = ((AddLicenseWindowViewModel)addLicWnd.DataContext).dialogResult;
        }

Form ViewModel

    public Window window;
    public event PropertyChangedEventHandler PropertyChanged;
    public LicenseRecordModel dialogResult;

    //Commands and OnPropertyChanged...

    public AddLicenseWindowViewModel(Window w)
    {
        window = w;
    }

    private void ButtonClick(){    
        dialogResult = new LicenseRecordModel("b", "b", "b", "b", "b");
        //...
        DialogCloser.SetDialogResult(wnd, true);
    }

This works but does not look MVVM compliant to me, what should I do? Did I misunderstand the answer?

Phoque
  • 217
  • 3
  • 14
  • You can use `ICommand` to bind the close command an set the dialog result – Pavel Anikhouski Nov 27 '19 at 09:37
  • If you read about MVVM, no one ever will ask you to be 100% MVVM compliant. MVVM is there to make you work better and as long as it does you should respect it, but if it doesn't you are completely free to break it. That's something that you will find in MVVM documentation as a guideline and I would respect it. But some people are 'purist' and you have the right to ask for a pure MVVM solution, just sometimes it doesn't make sense... – Ivan Ičin Nov 27 '19 at 09:37
  • "from my main view model, I instanciate my form window" - is already a violation of MVVM. view models cannot reference views. also it doesn't make sence to have `ButtonClick` not in the window, MVVM or no – ASh Nov 27 '19 at 09:41
  • Well, yes I just realised I could just call the Close() function of the window since I have that reference. But my problem is rather that I don't really want to have this reference in my VM ... – Phoque Nov 27 '19 at 09:48
  • @ASh Yes the Button click is actually a Command (just renamed it to explain this is what is called when I want to submit my form and close the window) – Phoque Nov 27 '19 at 09:49
  • @ASh To instanciate a window and to have a reference is not the same. Also having a reference is not a violation, it depends what type of reference do you have. See my post [Is MVVM pattern broken?](https://stackoverflow.com/questions/42931775/is-mvvm-pattern-broken). – Rekshino Nov 27 '19 at 09:49
  • May be this can be useful for you: [Open File Dialog MVVM](https://stackoverflow.com/a/43756154/7713750) – Rekshino Nov 27 '19 at 10:15
  • You have to go through the all answers in the post you have refered. [Attached trigger](https://stackoverflow.com/a/35267605/7713750) – Rekshino Nov 27 '19 at 10:51
  • @Rekshino oh, I love it will definitely try it out and see! – Phoque Nov 27 '19 at 11:22
  • You can also use the NuGet package "MvvmDialogs" and forget all problems one has to face when it comes to opening a dialog. – FantasticFiasco Nov 30 '19 at 15:05

0 Answers0