【发布时间】:2014-02-23 06:39:10
【问题描述】:
在我的程序中尝试将Command 绑定到MenuItem 时,我发现Commands 不能与MenuItems 一起使用,就像它们与其他控件一样。我一直使用this 帖子作为指导,但到目前为止还没有运气。基本上我的目标是在点击MenuItem 时运行Command。
这是我查看前面提到的帖子后的 xaml。我的Command 叫CreateFiles:
<MenuItem Header="{DynamicResource save}" Command="{Binding Path=PlacementTarget.DataContext.CreateFiles, RelativeSource={RelativeSource AncestorType=ContextMenu}}" />
我的Command 是在窗口的 ViewModel 中创建的,并且声明正常,但我还是会发布它:
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">...
【问题讨论】:
-
menuItem 声明在哪里?
-
它就在 XAML 中。它是
<Button.ContextMenu>的一部分 --><ContextMenu>
标签: c# wpf data-binding command menuitem