目前,我仍在摸索 WPF,无法弄清楚为什么禁用此上下文菜单项:
<Window x:Class="DisabledMenuItemProblem.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DisabledMenuItemProblem"
Title="Window1" Height="300" Width="300">
<TextBlock Text="fooooobaaaaaar">
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem Header="Foo" Command="{x:Static local:MyCommands.FooBar}" />
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</Window>
using System.Windows;
using System.Windows.Input;
namespace DisabledMenuItemProblem
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
CommandBindings.Add(new CommandBinding(MyCommands.FooBar, FooExecuted, CanFooExecute));
}
public void FooExecuted(object sender, ExecutedRoutedEventArgs e)
{ MessageBox.Show("Foo!"); }
public void CanFooExecute(object sender, CanExecuteRoutedEventArgs e)
{ e.CanExecute = true; }
}
public static class MyCommands
{
public static RoutedCommand FooBar = new RoutedCommand();
}
}
我错过了什么?
让我感到困惑的是,如果我在窗口中抛出一个按钮并将其命令设置为 FooBar 它就可以工作,一旦它被执行,上下文菜单就会启用!
干杯,克里斯。