0

I have a datagrid in my view(UserControl) in which I have a button, The requirement is to show a window on click of that button. I achieved it by creating an object for the window and calling it's show method and yes it's a wrong way, So, I want a standard way of achieving it.

I'm using Prism framework, So I mapped the window view with Window viewmodel using ViewModelLocationProvider in the windowviewmodel.

var table = new Window();

table.Content = new windowViewModel();

table.Show();

This is the code I implemented in the main viewmodel on click of the button.

It is actually opening a window, but, view is not getting loaded on the window.

3
  • Why not use the IDialogService (new in version 7)
    – Joe B
    Sep 13, 2019 at 3:35
  • Can you please help me on how to use IDialogService! Sep 13, 2019 at 4:48
  • look at the answer I wrote
    – Joe B
    Sep 13, 2019 at 15:11

2 Answers 2

1

Use the IDialogService Introduced in Prism 7

first the dialog view or ViewModel needs to implement the IDialogAware interface like this:

   class PopupViewModel : BindableBase, IDialogAware
   {
          public string Title => "Popup";
          public event Action<IDialogResult> RequestClose;

    public bool CanCloseDialog()
    {
        return true;
    }

    public void OnDialogClosed()
    {
    }
    public DelegateCommand OkCommand => new DelegateCommand(() =>
    {
        //you can also pass any parameters you want to back pass to the caller
        RequestClose.Invoke(new DialogResult(ButtonResult.OK)); 
    });

    public void OnDialogOpened(IDialogParameters parameters)
    {
       //here you get the parameters you passed;
    }
  }

then you need to register your view like this

  containerRegistry.RegisterDialog<Views.Popup>();

to use you inject into your ViewModel the IDialogService and call it

       this.dialogService.ShowDialog(nameof(Views.Popup), dialogParmaters, (result)=> 
      {
              if(result.Result == ButtonResult.OK)
              {
              }
      });

you can also register your custom window if you want the window needs to implement IDialogWindow

         containerRegistry.RegisterDialogWindow<MyDialogWindow>();
2
  • Will It open multiple windows, I should also restrict opening of window when a window is already opened. Sep 16, 2019 at 6:17
  • depends what you call Show or ShowDialog
    – Joe B
    Sep 16, 2019 at 16:48
1

First, you want the view model to be the DataContext (the Content is in fact the view). I'd advise againt the ViewModelLocator here, because it makes it impossible to pass parameters to the new window. Probably, you want to show different content depending on which row you click the button in.

new Window { DataContext = new WindowViewModel( myRow ) }.Show();

Second, there's nothing wrong with this approach but for one minor remark: put the code that shows the window in a service. You want to unittest your view models and that doesn't go well with opening real windows.

public interface IWindowService
{
    void ShowWindow( SomeParameter parameter );
}

If you inject that into your view model, you can still test it (verify that ShowWindow is called).

Third, remove calls to new and replace them with calls to injected factories.

var viewModel = _viewModelFactory.Create( "some parameter" );

is a lot nicer than

var viewModel = new ViewModel( "some parameter", new ADependency( new OtherDependency(), new SomeService() );

not to speak of the difficulties if singletons are involved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.