1

I have created a view (userControl), with a data entry form. I also have a view model bound to it with all the logic. When the button is pressed it sends the data off to the server (I'm using a RelayCommand for the button) via a ButtonCommand() Method i created.

At the end of this method i want to do a:

this.Close()

but obviosly as "this" isn't referring to the view, it doesnt work.

Is there any way of closing the window without using Codebehind the view?

Thanks in advance

mattisrowe
  • 149
  • 2
  • 14

1 Answers1

0

have a command like this

Command="{Binding CloseWindowCommand, Mode=OneWay}" 
CommandParameter="{Binding ElementName=MyWindow}"

then in handler close it !

Muds
  • 4,006
  • 5
  • 31
  • 53
  • But then your view model knows about the view if you are passing it as a `CommandParameter`. I would suggest your view implement an interface with a `Close()` method on it which your view implements. You then pass a reference to the view as an interface in the constructor of the view model and your view model can call the `Close()` method without knowing its implementation – JKennedy Mar 11 '15 at 14:35
  • ViewModel constructor, is something I haven't used in ages to get type of view. even if you use interfave you are passing information from view which defeats mvvm in one way or other ! – Muds Mar 11 '15 at 14:40
  • @user1 An interface isn't really necessary unless he is planning on implementing `Close()` on `UserControl`'s too. – Mike Eason Mar 11 '15 at 14:45
  • 1
    if you are not following the mvvm pattern it defeats the point of splitting your application up into views and vms. The whole point is so that you can re-use viewmodels across different programs. For example your view model will work fine when closing a wpf window, but when you go to implement the view model with an asp.net application it trips up. Where as using dependency injection with an interface you just have to implement the close method on your asp.net page and everything else is exactly the same – JKennedy Mar 11 '15 at 14:49
  • hmm.. Yea, that's correct ! – Muds Mar 11 '15 at 14:53