How to close a window from viewmodel?

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

See subject.

Jul 13, 2012 at 11:39 AM

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

Jul 24, 2012 at 1:52 PM
Edited Jul 24, 2012 at 1: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 6: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 9:00 AM
Edited Jul 27, 2012 at 9: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 9:22 AM

Then Save() fails because you pass true.

Jul 27, 2012 at 9:26 AM

When I pass false, the same happens: Nothing.

Jul 27, 2012 at 9:43 AM

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

Jul 27, 2012 at 10:27 AM

Repro and issue created:

Jul 27, 2012 at 11:16 AM

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

Jul 27, 2012 at 3:49 PM


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

Jul 28, 2012 at 10: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 10:31 AM
Edited Jul 31, 2012 at 8: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 3: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?