I'm new to MVVM and run into the problem how to open a dialog and close afterwards the dialog in its own ViewModel c#-file. Search for possible solution and found nothing suitable. My solution looks as follows, but I'm not sure if this has some drawbacks. Defined a UserControl and open it with:
void ChangeDataPathExecute()
{
Window window = new Window
{
Content = new ChangeDataRootPathUserControl(),
};
window.ShowDialog();
}
In the ViewModel of the UserControl file implement:
private void DetermineMyWindow()
{
foreach (Window window in App.Current.Windows)
{
ChangeDataRootPathUserControl uc = window.Content as ChangeDataRootPathUserControl;
if (uc == null)
continue;
myWindow = window;
}
and finally in the Close method:
void OkChangeDataRootPathExecute()
{
DetermineMyWindow();
myWindow.Close();
}
What do you think about this? Hack or good solution? Thanks for feedback Beat