WPF - MVVM : prevent multiple calls of Dialogs

WPF - MVVM : prevent multiple calls of Dialogs

melomayo6BC8R
Contributor Contributor
522 Views
2 Replies
Message 1 of 3

WPF - MVVM : prevent multiple calls of Dialogs

melomayo6BC8R
Contributor
Contributor

How can I prevent a modeless dialog created as WPF from being called again until it has been closed?

0 Likes
Accepted solutions (2)
523 Views
2 Replies
Replies (2)
Message 2 of 3

norman.yuan
Mentor
Mentor
Accepted solution

Using modeless dialog/window/PalleteSet is tricky.

Typically, it should be instantiated as singleton object in an AutoCAD session, something like:

 

public class MyCommandClass

{

   private static MyWindow _window=null

   [CommandMethod("ShowMyWindow", CommandFlags.Session)]

   publci static void ShowMyWindow()

   {

      If (_window==null)

      {

         _window=new MyWindow();

      }

      Application.ShowModelessWindow(_window);

   }

}

 

In this case, you would want to handle Window.Closing event, where you cancel the closing and set its Visibility property to Hidden.

 

That is, one the window is instantiated, it stays in the AutoCAD session, being either hidden or visible. 

Obviously, id the content of the window is drawing specific, you need to reset the content whenever current drawing changes.

 

You can also instantiate the modeless window on "per-document" basis (you could take advantage of [PerDocumentClass]. 

 

These old articles might be of help:

https://drive-cad-with-code.blogspot.com/2014/02/showing-and-closing-modeless-formdialog.html 

https://drive-cad-with-code.blogspot.com/2018/09/showing-modeless-form-with-help-of.html 

 

Since you use MVVM pattern, you can let the ViewModel to subcribe DocumentBecameCurrent event and update the ViewModel, which in turn update the window view.

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 3

melomayo6BC8R
Contributor
Contributor
Accepted solution

Hi Norman,
many heartfelt thanks for that. Duplicate dialog calls are prevented by a static variable.

0 Likes