How to close a window from viewmodel?

Topics: Questions
Jul 13, 2012 at 10:35 AM

See subject.

Jul 13, 2012 at 10:39 AM

By using this method in your View Model: CloseViewModel(true);

Jul 24, 2012 at 12:52 PM
Edited Jul 24, 2012 at 12:56 PM

Yes I know this, but this doesn't work.

If I show a viewModel with:


GetService<IUIVisualizerService>().Show(new AdditionalInputViewModel(SelectedTicket));


Then in the AdditionalInputViewModel the window didn't get closed with CloseViewModel(true), if I create the view myself than it will work. What can I do?

Edit: With .ShowDialog it works, but not with show, why?

Jul 25, 2012 at 5:56 PM

Probably because Catel is trying to set the DialogResult, and that is hard when you call a window with Show. If you send a repro, we will look into it.

Jul 27, 2012 at 8:00 AM
Edited Jul 27, 2012 at 8:00 AM

Have the same problem. But in my case it also don't works with ShowDialog().

var viewModel = new MyViewModel();
var uiVisualizerService = GetService<IUIVisualizerService>();

In a command in the MyViewModel I call CloseviewModel(true). But nothing happens.

Jul 27, 2012 at 8:22 AM

Then Save() fails because you pass true.

Jul 27, 2012 at 8:26 AM

When I pass false, the same happens: Nothing.

Jul 27, 2012 at 8:43 AM

Please provide a repro, I have never seen this behavior before.

Jul 27, 2012 at 9:27 AM

Repro and issue created:

Jul 27, 2012 at 10:16 AM

Oh. CloseViewModel only works with DataWindow from catel not with a normal Window?

Jul 27, 2012 at 2:49 PM


I never used Catel DataWindow, just normal window with WindowBehavior, and showDialog works for me.

Jul 28, 2012 at 9:37 PM

You are using a custom window. You are not using either the DataWindow nor the WindowBehavior, and these are the classes that watch the view model.

If you want to use your own views, you must do all of this yourself. The view model has no reference to the view, so it cannot close the view. Internally, the views of Catel subscribe to the viewmodel and close itself when the viewmodel raises the Close event.

Jul 30, 2012 at 9:31 AM
Edited Jul 31, 2012 at 7:42 AM

? I use normal window with windowbehavior. And closeViewModel works, if it was shown with showDialog. Or what do your mean?

Jul 30, 2012 at 2:23 PM

I checked the issue jens_weller uploaded. He provided a reproducable issue, and that one contained a simple window without any behaviors.

@mrmiagi0101: can you try setting ForceCloseAfterSettingDialogResult to true?