Quantcast
Viewing all articles
Browse latest Browse all 18858

How to close View from ViewModel without using code behind?

My application is showing a View as though it were a modal dialog. This view can be closed as a result of the user clicking on an OK or Cancel button. Because the OK closure has work to do in the VM the VM has to catch the actions and For the life of me, I just can't get that to happen without doing something that violates MVVM or otherwise is contrary to the MVVM way of doing things.

My first approach was to set the OK and Cancel buttons as Default and Cancel. This did nothing.

Next I tried to bind the Window.DialogResult property to a property in my VM. This, of course, failed apparently because the DialogResult in the View is not a dependency property.

The next approach was to craft my button commands so that they passed a Window object reference down the command as a parameter. Sadly, I just couldn't get the RelayCommand syntax right to accept that.

After that I tried to follow Joe White's idea (link to his blog) which looked promising but I couldn't get that to work either. His idea was to create an attached property to which Window.DialogResult was bound. Somehow the VM was able to write to that attached property but I was unable to get his code to work for me. My VM caught the button click and set the VM DialogResult property but there was no wiring to hook this to back to the attached property so the View was unaffected.

So - any ideas how to close a View from its ViewModel without violating MVVM precepts?


Richard Lewis Haggard


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>