I spent most of today trying to make sense of the MANY different approaches to closing a dialog in WPF/MVVM. Most answers focus on simple dialogs - like a Yes/No confirmation dialog. However, no one seems to describe a more complicated case of a dialog that actually performs some actions, which have to be committed in a transactional way: all-or-nothing.
There is an OK button, clicking which invokes a bound command on the ViewModel. All is fine.
Now, I want to close the window if the command succeeds, but I don't want to close it, if the command fails.
After hours of research I came to the conclusion that apparently no one in the world has ever had a similar problem to solve :|
The solution that I invented, but have not implemented yet is as follows:
I would have a
ConditionalCloseWindowcommand, which takes the window as a parameter and, well, closes the window.The
ConditionalCloseWindowcommand would have an attached property:public static readonly DependencyProperty Condition = DependencyProperty.RegisterAttached("Condition", typeof(ICommand), ... snipExecuting the
ConditionalCloseWindowcommand would first cause the execution of theConditioncommand. Only on successful execution of theConditioncommand would theConditionalCloseWindowactually perform theClose().
What do you think of such solution? Is it totally invalid? Or maybe it is an accepted pattern, that I have not succeeded to discover through my research?