我的示例 WPF 应用程序中的 AppMenus 有问题。
Window2.xaml:
<Window x:Class="SampleWpfApp.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SampleWpfApp"
Name="RootWindow"
Title="Window2" Height="600" Width="800">
<Window.InputBindings>
<KeyBinding Gesture="CTRL+N" Command="ApplicationCommands.New" CommandTarget="{Binding ElementName=TopMenu}" />
<KeyBinding Gesture="CTRL+F1" Command="{x:Static local:TopMenu.ShowHelp}" CommandTarget="{Binding ElementName=TopMenu}" />
</Window.InputBindings>
<DockPanel>
<local:TopMenu DockPanel.Dock="Top" x:Name="TopMenu" />
<ContentControl>
<local:Home x:Name="MainContent" />
</ContentControl>
</DockPanel>
TopMenu.xaml
<UserControl x:Class="SampleWpfApp.TopMenu"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:SampleWpfApp"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.InputBindings>
<KeyBinding Gesture="CTRL+N" Command="ApplicationCommands.New" />
<KeyBinding Gesture="CTRL+F1" Command="{x:Static local:TopMenu.ShowHelp}" />
</UserControl.InputBindings>
<UserControl.CommandBindings>
<CommandBinding Command="ApplicationCommands.New" Executed="NewExecuted" CanExecute="NewCanExecute"/>
<CommandBinding x:Name="HelpCmdBinding" CanExecute="AltHelpCanExecute" Executed="AltHelpExecuted" Command="{x:Static local:TopMenu.ShowHelp}" />
</UserControl.CommandBindings>
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Command="ApplicationCommands.New" />
<MenuItem Header="E_xit" InputGestureText="Alt+F4" />
</MenuItem>
<MenuItem Header="_Help">
<MenuItem Header="_View Help" InputGestureText="Ctrl+F1" Command="{x:Static local:TopMenu.ShowHelp}" />
<MenuItem Header="_About" />
</MenuItem>
</Menu>
</DockPanel>
TopMenu.xaml.cs
public partial class TopMenu : UserControl
{
public static RoutedCommand ShowHelp = new RoutedCommand("AltHelp", typeof(TopMenu));
public TopMenu()
{
InitializeComponent();
}
void NewExecuted(object target, ExecutedRoutedEventArgs e)
{
MessageBox.Show("The " + ((RoutedCommand)e.Command).Name + " command invoked on " + ((FrameworkElement)target).Name);
}
void NewCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
void AltHelpExecuted(object target, ExecutedRoutedEventArgs e)
{
MessageBox.Show("The " + ((RoutedCommand)e.Command).Name + " command invoked on " + ((FrameworkElement)target).Name);
}
void AltHelpCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
}
主页.xaml
<UserControl x:Class="SampleWpfApp.Home"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" Margin="10,37,0,0"/>
<TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" Margin="10,86,0,0"/>
<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="10,127,0,0"/>
</Grid>
运行应用程序。确保不要单击文本框或文本框的选项卡。单击文件菜单。菜单已启用。检查查看帮助菜单。它也已启用。当您单击时,您会看到消息框。万事皆安。
但是当我单击文本框时,菜单被禁用。在我重新启动应用程序并且不单击文本框之前,我无法再次启用菜单。(尽管使用手势仍然会触发消息框)。有人可以帮我确定问题吗?这让我发疯了一段时间:(