0

I have a WPF window, datacontext is instantiated usin XAML

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ViewModels="clr-namespace:Contratos.ViewModels" x:Class="Contratos.Views.TipoAsociadoAcopio"
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
    xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF45"
    xmlns:l="clr-namespace:Recursos;assembly=Recursos"
    xmlns:controls="clr-namespace:Recursos.Controls;assembly=Recursos"
    xmlns:resources="clr-namespace:ModelSeguridad.Resources;assembly=ModelSeguridad"
    Title="{x:Static resources:Labels.CONTRATO_TipoContratoAcopio}" Height="Auto" Width="Auto">
<Window.Resources>
    <CollectionViewSource x:Key="ListaItems" Source="{Binding ListaItems}"/>
    <ViewModels:TipoAsociadoVM x:Key="ViewDataContext"/>     
</Window.Resources>
<Grid>
    <StackPanel>
        <Button Command="{Binding _ICommandExit}" CommandParameter="{W H A T   H E R E}" />
    </StackPanel>
</Grid>

I need to close this window when the user click on Exit Button, My question is How can I do to send the Window reference to viewmodel if it was instantiated using XAML.

I'm trying to maintain MVVM pattern, thats because I don't have any code on mi codebehind.

Juan Pablo Gomez
  • 5,203
  • 11
  • 55
  • 101
  • 1
    Personally, I think it's totally acceptable to put a button click handler in the code behind rather than binding to a command. Closing the window is purely a UI task, so you're still keeping the view logic decoupled. – zmb Oct 02 '13 at 16:06
  • @zmb Tks, yes you have the reason. But I like to automate this task for all my Windows, plus some log must be trapped when the user close the window. – Juan Pablo Gomez Oct 02 '13 at 16:10
  • That's not MVVM. Just close the window in the UI. –  Oct 02 '13 at 18:19
  • It's a shame that those users decided to close this question, saying that it was a duplicate of that other post, because if they'd actually read the two posts, they'd all see that they were mistaken. This question is *not* about closing the `MainWindow` like the other question, so an answer showing how to close the `MainWindow` does *not* answer this question. – Sheridan Oct 02 '13 at 20:37

3 Answers3

0

ViewModel should not be having the window reference, and command should not be sending it in its params. If you just want to close your window you can do it in code behind or if you still want to do it in command then you can find your window reference in Application.Current.Windows in command handler and close it.

Nitin
  • 18,344
  • 2
  • 36
  • 53
0

Although I agree with what the guys said about it being OK to use the code behind in MVVM, I have a possible solution for you.

First, declare the Name property of your Window. Then you can access the window from just about anywhere like this:

Window window = Application.Current.Windows.OfType<Window>().Where(w => w.Name ==
"WindowName").FirstOrDefault();
if (window != null) window.Close();

I would also agree that the view model is not the place to do this, but it's your code. :)

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • Tks for your hel, but what abouit if you have more tan one instance of the same type of window? – Juan Pablo Gomez Oct 02 '13 at 17:16
  • Give them different names and make sure you keep a record of them. When you want to close, one just substitute the name of the relevant `Window` with `WindowName` in my example code and it will find and close that `Window`. – Sheridan Oct 02 '13 at 20:26
0
<Button Command="{Binding _ICommandExit}" 
       CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=Window}}"/>
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Taras
  • 1,118
  • 2
  • 12
  • 19