我看到了这个,所以问题,但当我尝试它不起作用。
我知道我可以执行mvvm,但是execute命令所做的是特定于视图的操作,所以我希望在视图上执行。
<Grid>
<TextBox>
<TextBox.ContextMenu>
<ContextMenu>
<MenuItem Header="_Copy" CommandTarget="{Binding Path=PlacementTarget,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ContextMenu}}}">
<MenuItem.CommandBindings>
<CommandBinding CanExecute="CommandBinding_CanExecute"
Executed="CommandBinding_Executed"
/>
</MenuItem.CommandBindings>
</MenuItem>
</ContextMenu>
</TextBox.ContextMenu>
</TextBox>
</Grid>代码背后:
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = false;
}我预计菜单将被禁用,但它不是:

我试过其他方法:
<!--Approach 2: CopyCommand as property of the Window. CopyCommand is in code-behind of the window-->
<MenuItem Header="_Copy" Command="{Binding Path=CopyCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>
<!--Approach 3: Saw other samples on the net about binding to PlacementTarget. CopyCommand is in code-behind of the window-->
<MenuItem Header="_Copy" Command="{Binding Path=PlacementTarget.CopyCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>发布于 2016-05-08 11:53:52
我的CopyCommand在视图的代码后面。
查看:
public ICommand CopyCommand { get { return _copyCommand; } }然后,当我将视图的实例作为指定的这里放在textbox的tag属性中时,我能够访问它
<views:myView>
<Grid>
<TextBox Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type views:myView}}}">
<TextBox.ContextMenu>
<ContextMenu DataContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
<MenuItem Header="_Copy" Command="{Binding CopyCommand}"/>
</ContextMenu>
</TextBox.ContextMenu>
</TextBox>
</views:myView>
</Grid>发布于 2016-04-26 07:31:36
如果您正在尝试实现WPF内置的ApplicationCommands.Copy属性,那么您需要做的就是
<TextBox>
<TextBox.ContextMenu>
<ContextMenu>
<MenuItem Command="ApplicationCommands.Copy"/>
</ContextMenu>
</TextBox.ContextMenu>
</TextBox>由于该命令与textbox相关联,它将自动处理执行并可以执行事件。
但是,如果您想实现您自己的自定义命令,则必须稍微冗长一些。
public static class MyCommands
{
public static readonly RoutedUICommand Copy= new RoutedUICommand
(
"Copy",
"_Copy",
typeof(MyCommands),
null
);
//You can define more commands here
}
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true; //can check with debugger...
}
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
//Your Action
}在你的xaml中
<Window.CommandBindings>
<CommandBinding Command="local:MyCommands.Copy" CanExecute="CommandBinding_CanExecute" Executed="CommandBinding_Executed" />
</Window.CommandBindings>
<Grid>
<TextBox>
<TextBox.ContextMenu>
<ContextMenu>
<MenuItem Command="local:MyCommands.Copy"/>
</ContextMenu>
</TextBox.ContextMenu>
</TextBox>
</Grid>发布于 2016-04-26 05:28:24
在Command中指定MenuItem,如下所示:
<MenuItem Command="ApplicationCommands.Copy" .../>还在Command中为CommandBinding属性指定一个值,
<MenuItem.CommandBindings>
<CommandBinding Command="ApplicationCommands.Copy" CanExecute="CommandBinding_CanExecute"
Executed="CommandBinding_Executed" />
</MenuItem.CommandBindings>https://stackoverflow.com/questions/36855188
复制相似问题