Saturday, October 6, 2012

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

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

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"); } 

Answers & Comments...

Answer: 1

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(); } 
by : aqwerthttp://stackoverflow.com/users/373706

Answer: 2

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.

by : nportellihttp://stackoverflow.com/users/7024

Answer: 3

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.

by : Rafal Spacjerhttp://stackoverflow.com/users/345006

Answer: 4
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(); } 
by : user841960http://stackoverflow.com/users/841960

Answer: 5

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

EDIT: Here is there Stackoverflow discussion

by : jmogerahttp://stackoverflow.com/users/891906

Answer: 6

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.
by : Paritosh Sonihttp://stackoverflow.com/users/1656887

Answer: 7

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.

by : bugged87http://stackoverflow.com/users/1575237




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog