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");
}
Jehof
  • 32,386
  • 9
  • 115
  • 149
Jérôme Oudoul
  • 339
  • 5
  • 14

8 Answers8

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.

bugged87
  • 2,678
  • 2
  • 18
  • 36
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();
}
LPL
  • 16,007
  • 6
  • 43
  • 87
user841960
  • 11
  • 1
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.
bool.dev
  • 16,879
  • 5
  • 62
  • 90
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();
    }
}
DanteTheEgregore
  • 1,440
  • 5
  • 22
  • 42
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();
}
aqwert
  • 9,594
  • 1
  • 35
  • 57
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.

nportelli
  • 3,786
  • 7
  • 35
  • 50
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.

Rafal Spacjer
  • 4,588
  • 2
  • 24
  • 32
0

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

EDIT: Here is there Stackoverflow discussion

Community
  • 1
  • 1
jmogera
  • 1,579
  • 3
  • 29
  • 63