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"); } 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/373706Answer: 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/7024Answer: 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.
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/841960Answer: 5
We have implemented a NO-CODE BEHIND functionality. See if it helps.
EDIT: Here is there Stackoverflow discussion
by : jmogerahttp://stackoverflow.com/users/891906Answer: 6
Here are some ways to accomplish it.
- Send message to your childwindow and set DialogueResult to false on childwindow code-behind.
- Make property of DialogueResult and Bind it with childwindow Dialoue CLR property, set it on CancelEval method of CancelCommand.
- Create object of Childwindow and set DialogueResult false on CancelEval.
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