如:
<MenuItem Header="hi" InputGestureText="Ctrl+E" Click="MenuItem_Click" /> private void MenuItem_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Testing");
}运行时按下Ctrl+E不能弹出对话框,点击可以跳出,请问是哪里出错了?
<MenuItem Header="hi" InputGestureText="Ctrl+E" Click="MenuItem_Click" /> private void MenuItem_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Testing");
}运行时按下Ctrl+E不能弹出对话框,点击可以跳出,请问是哪里出错了?
你自己写的Command还是用系统的Command?看看Command的CanExecute方法里有什么限制条件,如果CanExecute返回false,则按钮会不可点击
{
InitializeComponent();
CommandBindings.Add(new CommandBinding(CustomCommands.ExitCommand, MenuItemClick, CanExecute));
} public void CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}这样子写是灰色的
写错了,应该这样
menuItem.CommandBindings.Add(new CommandBinding(CustomCommands.ExitCommand, MenuItemClick, CanExecute));
public static RoutedCommand ExitCommand= new RoutedCommand();
public Window1()
{
InitializeComponent();
} // ExecutedRoutedEventHandler for the custom color command.
private void ExitCommandExecuted(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show(e.OriginalSource.ToString());
} // CanExecuteRoutedEventHandler for the custom color command.
private void ExitCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true; }XAML
<Window...
xmlns:custom="clr-namespace:Sample"/>
...
...
<Window.CommandBindings>
<CommandBinding Command="{x:Static custom:Window1.ExitCommand}"
Executed="ExitCommandExecuted"
CanExecute="ExitCommandCanExecute"/>
</Window.CommandBindings>
...
...
<MenuItem Header="Color Command"
Command="{x:Static custom:Window1.ExitCommand}" />
</MenuItem>