我有一个简单的WPF窗口:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.New" Executed="NewCommand_Executed"/>
</Window.CommandBindings>
<Grid>
<Menu Name="muMain" Margin="0,0,0,288">
<MenuItem Header="File">
<MenuItem Name="mnFileNew" Command="ApplicationCommands.New"/>
</MenuItem>
</Menu>
<Button Name="btnNew" Content="Button" HorizontalAlignment="Left" Height="39" Margin="22,70,0,0" VerticalAlignment="Top" Width="439" Command="ApplicationCommands.New"/>
</Grid>
</Window>
和后面的代码来测试ExecutedRoutedEventArgs.Source:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
void NewCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
Control bt = e.Source as Control;
Console.WriteLine("New command triggered by " + bt.Name);
}
}
如果我点击文件 - &gt;新增,输出为“由 mnFileNew 触发的新命令”,然后单击按钮,输出为“由 btnNew 触发的新命令”。这一切都很好。但如果我点击文件 - &gt;再次换新,输出仍为“由 btnNew 触发的新命令”。谁能告诉我为什么?