简体   繁体   中英

Close Dialog with Prism Region Manager

I have a WPF/PRISM application in which I opened a window using the following code:

RegionManager.RequestBlockingDialogNavigate(MyViewModel);

How do I close that window without using code behind?

Thanks.

I could achieved my goal doing something else. Basically I just pass the whole window as a parameter of the CloseCommand and close it from the ViewModel.

<Button Content="Close" Command="{Binding CloseCommand}" CommandParameter="{Binding ElementName=MyWindow}" />

And in the View Model, I just did this:

    public DelegateCommand<Window> CloseCommand { get;  private set;} 
    public MyViewModel()
    {
        CloseCommand = new DelegateCommand<Window>(Close);
    }
    public void Close(Window window)
    {
        window.Close();
    }

I thought there was something in PRISM to do it. I haven't found anything so far.

Thanks

You could also try this,

<Button Content="Close" Command="{Binding CloseCommand}"  />

public DelegateCommand CloseCommand { get;  private set;} 
public MyViewModel()
{
    CloseCommand = new DelegateCommand(()=>
    {
          foreach (Window window in Application.Current.Windows)
                {
                    if (window is DialogView)//your window type come here
                    {
                        window.Close();
                    }                  }                                                                                        

    });
}

This will not work if window was shown with .ShowDialog();

or instead of foreach, this where dialog view is your window

Application.Current.Windows.OfType<DialogView>().FirstOrDefault().Close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM