WPF - CanExecute不适用于DataGrid的ContextMenu [英] WPF - CanExecute doesn't work for DataGrid's ContextMenu
问题描述
我想让我的DataGrids有一个上下文菜单,允许用户向上或向下移动所选行。经过一番搜索,我想出了以下代码有问题。看来 CanExecute
只被调用一次(我想当菜单被创建),所以我的菜单项将被启用或禁用所有的时间。我所努力实现的是使它启用时,在DataGrid中选择的行和禁用时,没有任何。目前这不工作。
I wanted to have my DataGrids with a context menu that allows the user to move the selected rows up or down. After some search, I've come up with the following code that has a problem. It seems that CanExecute
is only called once (I guess when the menu gets created), so my MenuItem will either be enabled or disabled all of the time. What I am struggling to achieve is to make it enabled when there are rows selected in the DataGrid and disabled when there aren't any. Currently this doesn't work.
此外,有没有更优雅的解决方案呢?如果我使用更多的代码,我认为它会变成一个巨大的混乱...
Also, is there a more elegant solution to this? If I use more code I think it will become a huge mess...
public class DataGridMoveRowsUpCommand : ICommand
{
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
var grid = parameter as DataGrid;
if (grid != null && grid.SelectedItems.Count > 0) return true;
return false;
}
public void Execute(object parameter)
{
// Do sth...
}
public void RaiseCanExecuteChanged()
{
if (CanExecuteChanged != null) CanExecuteChanged(this, EventArgs.Empty);
}
}
//-------------------------------------------------------------
public class MyDataGrid : DataGrid
{
public static ICommand DataGridMoveRowsUp
{
get { return new DataGridMoveRowsUpCommand(); }
}
}
//-------------------------------------------------------------
<kbm:MyDataGrid x:Name="gridExpenses" ContextMenu="{StaticResource DataGridContextMenu}"/>
//-------------------------------------------------------------
<ContextMenu x:Key="DataGridContextMenu" x:Shared="true">
<MenuItem Header="{DynamicResource StringMoveUp}"
Command="kbm:MyDataGrid.DataGridMoveRowsUp"
CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"
CommandParameter="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}">
</MenuItem>
...
推荐答案
调用 RaiseCanExecuteChanged
方法,只要你觉得那个命令需要重新评估。 (最有可能的情况是 SelectedItems
的dataGrid更改了。)
You have to manually called RaiseCanExecuteChanged
method whenever you feel like that command needs to be revaluated. (Most probably in case SelectedItems
of dataGrid gets changed.)
或
您可以让 CommandManager
决定何时通过挂接到 CanExecuteChanged
code> RequesterySuggested 事件如下:
Either you can let CommandManager
decides when to raise CanExecuteChanged
event of your command by hooking onto RequerySuggested
event of CommandManager like this:
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
RequerySuggested
event当
System.Windows.Input.CommandManager检测到可能
改变命令执行能力的条件时发生。
RequerySuggested
event Occurs when the System.Windows.Input.CommandManager detects conditions that might change the ability of a command to execute.
这篇关于WPF - CanExecute不适用于DataGrid的ContextMenu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!