试图在我的程序中将Command
绑定到MenuItem
,我发现Commands
不像MenuItems
那样与其他控件一起使用。我一直在使用this作为指南,但迄今为止还没有运气。基本上我的目标是在单击MenuItem
时运行Command
。如何在MenuItems上执行命令
这是我查看之前提到的帖子后的xaml。我Command
被称为CreateFiles
:
<MenuItem Header="{DynamicResource save}" Command="{Binding Path=PlacementTarget.DataContext.CreateFiles, RelativeSource={RelativeSource AncestorType=ContextMenu}}" />
我Command
在窗口的视图模型被创建并宣布正常人一样,但我无论如何都会张贴:
private ICommand _createFiles;
public MainWindowViewModel()
{
_createFiles = new Command(createFiles_Operations);
}
public ICommand CreateFiles { get { return _createFiles; } }
private void createFiles_Operations()
{
}
为了测试是否不是我Command
工作我在第一个护腕处设置了一个断点。到目前为止,当点击MenuItem
时,程序还没有停在这个中断点。
由于此方法似乎不起作用,我该怎么做Commands
与MenuItems
一起使用?
更新:Command
改为ICommand
更新2:ContextMenu
& Button
XAML:
<Button Click="Button_Click_1" Margin="5,4,0,0" Name="Button_1" Height="55" VerticalAlignment="Top" HorizontalAlignment="Left" Width="55" BorderBrush="Black">...
<ContextMenu x:Name="MainContextMenu" MouseLeave="ContextMenuMouseLeave" Background="White" BorderBrush="#FF959595" SnapsToDevicePixels="False">...
在哪里菜单项申报? – Botonomous
它就在XAML中。它是'' - >'' –