Question
Why is the WPF ApplicationCommands.Close command disabled on the button and in the menu item?
Code
<Window x:Class="ApplicationCloseCommand.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="clr-namespace:ApplicationCloseCommand.ViewModels"
mc:Ignorable="d"
Title="MainWindow" Height="169.493" Width="319.273">
<Window.DataContext>
<viewModels:MainWindowViewModel />
</Window.DataContext>
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Header="_Exit" Command="Close"/>
</MenuItem>
</Menu>
<Canvas>
<Button Content="Close" Command="Close" Height="23" VerticalAlignment="Top" Canvas.Left="142" Canvas.Top="31"/>
</Canvas>
</DockPanel>
</Window>
Research
Some sources say to implement an ICommand
yourself and do a this.Close()
in the command handler. But then, I don't understand why ApplicationCommands.Close
exists at all.
Some sources say that one needs to implement a CanExecute
methode to return true
. But again, if I have to do that myself including the CloseCommandHandler
(from the linked example), what's the benefit of ApplicationCommands.Close
?
Some sources mention a CommandTarget
. This didn't work:
<MenuItem Header="_Exit" Command="Close" CommandTarget="{Binding ElementName=MyMainWindow}"/>
Did I just do it wrong with the CommandTarget
?