How to close a ChildWindow with Cancel button using MVVM Light Toolkit
Asked Answered
M

8

7

I'm new to MVVM and trying to figure out how to close a ChildWindow with the traditional Cancel button using MVVM Light Toolkit.

In my ChildWindow (StoreDetail.xaml), I have :

<Button x:Name="CancelButton" Content="Cancel" Command="{Binding CancelCommand}" />

In my ViewModel (ViewModelStoreDetail.cs), I have :

public ICommand CancelCommand { get; private set; }

public ViewModelStoreDetail()
{
    CancelCommand = new RelayCommand(CancelEval);
}

private void CancelEval()
{
    //Not sure if Messenger is the way to go here...
    //Messenger.Default.Send<string>("ClosePostEventChildWindow", "ClosePostEventChildWindow");
}
Mireielle answered 12/4, 2011 at 15:56 Comment(0)
P
1
private DelegateCommand _cancelCommand;

public ICommand CancelCommand
{
    get
    {
        if (_cancelCommand == null)
            _cancelCommand = new DelegateCommand(CloseWindow);
        return _cancelCommand;
    }
}

private void CloseWindow()
{
    Application.Current.Windows[Application.Current.Windows.Count - 1].Close();
}
Pinup answered 13/7, 2011 at 4:12 Comment(0)
A
1

If you displayed your child window by calling ShowDialog(), then you can simply set the IsCancel property of your button control to "True".

<Button Content="Cancel" IsCancel="True" />

It becomes the same as clicking the X button on the window, or pressing ESC on the keyboard.

Arpent answered 19/9, 2012 at 22:22 Comment(0)
S
0

Have a look at this articleon MSDN. About half way down there is an approach on how to do this. Basically it uses either uses a WorkspaceViewModel or you implements an interface that exposes and event RequestClose

You then inside the Window's DataContext (if you are setting the ViewModel to it) you can attach to the event.

This is an excerpt from the article (Figure 7). You can adjust it to suit your needs.

// In App.xaml.cs
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);

MainWindow window = new MainWindow();

// Create the ViewModel to which 
// the main window binds.
string path = "Data/customers.xml";
var viewModel = new MainWindowViewModel(path);

// When the ViewModel asks to be closed, 
// close the window.
viewModel.RequestClose += delegate 
{ 
    window.Close(); 
};

// Allow all controls in the window to 
// bind to the ViewModel by setting the 
// DataContext, which propagates down 
// the element tree.
window.DataContext = viewModel;

window.Show();
}
Strychnine answered 12/4, 2011 at 18:9 Comment(0)
C
0

It's been a while since I've used WPF and MVVMLight but yes I think I'd use the messanger to send the cancel event.

Chibouk answered 12/4, 2011 at 20:29 Comment(0)
B
0

In MVVM Light Toolkit the best what you can do is to use Messenger to interact with the View.

Simply register close method in the View (typically in the code behind file) and then send request to close a window when you need it.

Bombard answered 12/4, 2011 at 20:51 Comment(0)
S
0

We have implemented a NO-CODE BEHIND functionality. See if it helps.

EDIT: Here is there Stackoverflow discussion

Shaver answered 12/8, 2011 at 23:58 Comment(0)
A
0

Here are some ways to accomplish it.

  1. Send message to your childwindow and set DialogueResult to false on childwindow code-behind.
  2. Make property of DialogueResult and Bind it with childwindow Dialoue CLR property, set it on CancelEval method of CancelCommand.
  3. Create object of Childwindow and set DialogueResult false on CancelEval.
Annis answered 8/9, 2012 at 16:38 Comment(0)
P
0

Kind of late to the party but I thought I'd add my input. Borrowing from user841960's answer:

public RelayCommand CancelCommand
{
    get;
    private set;
}

Then:

SaveSettings = new RelayCommand(() => CloseWindow());

Then:

private void CloseWindow()
{
    Application.Current.Windows[Application.Current.Windows.Count - 1].Close();
}

It's a bit cleaner than using an ICommand and works just as well.

So, to sum it all up, the example class would look like so:

public class ChildViewModel
{
    public RelayCommand CancelCommand
    {
        get;
        private set;
    }

    public ChildViewModel()
    {
        SaveSettings = new RelayCommand(() => CloseWindow());
    }

    private void CloseWindow()
    {
        Application.Current.Windows[Application.Current.Windows.Count - 1].Close();
    }
}
Pyroxylin answered 14/8, 2013 at 18:15 Comment(2)
Where would the SaveSettings = ... go?Ms
In the ViewModel constructor. A full example of this can be found here along with Command, RelayCommand, and EventToCommand documentation.Pyroxylin

© 2022 - 2024 — McMap. All rights reserved.