Close ModernWindow from anotherViewModel

May 2, 2014 at 10:54 AM
I have a requirement where I've to open a ModernWindow from a View (from ViewModel)..On that ModernWindow I've a submit button. I want to close that window after submit button is clicked. How to achieve this? Calling the below code from XYZViewModel
                        var _dataContext = new ABCViewModel();
                        var wnd = new ModernWindow
                        {
                            Style = (Style)App.Current.Resources["EmptyWindow"],
                            Content = new Suppliers
                            {
                                Margin = new Thickness(32)
                            },
                            DataContext = _dataContext,
                            Width = 500,
                            Height = 500
                        };
                        wnd.Show();

Any help is appreciated. Thanks a lot.
May 12, 2014 at 2:19 PM
Edited May 12, 2014 at 2:21 PM
Hello, you can solve this passing the UserControl in CommandParameter of buttom and inside command write:

XAML USER CONTROL
<UserControl x:Class="UserControl1View"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
...
            x:Name="_userControl"
...
XAML OF BUTTON
<Button Command="{Binding CommitCommand}"
                CommandParameter="{Binding ElementName=_userControl}"
                Content="Commit"/>
BEHIND CODE:
public ICommand CommitCommand
{
    get
    {
        return new RelayCommand<UserControl>(userControl =>
        {
            var win = Window.GetWindow(userControl);
            if (win != null)
                win.Close();
        });
    }
}