菜单项命令绑定到位于“窗口”中的“用户控件”中的视图模型

本文关键字:用户控件 控件 模型 视图 用户 中的 命令 菜单项 窗口 绑定 | 更新日期: 2023-09-27 17:57:00

我有一个带有Grid的窗口:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="0.5*" />
        <RowDefinition Height="40" />
        <RowDefinition Height="15*" />
        <RowDefinition Height="0.3*" />
    </Grid.RowDefinitions>
    <central:CentralView Grid.Row="2" ItemsSource="{Binding MainWindowModels}" />
</Grid>

CentralView 是具有 DataGrid 和 DataGrid ContextMenu 的用户控件。此外,我有简单的类女巫实现 ICommand 接口和静态命令类,其中存储了所有命令:

static CentralViewBaseCommands _goToEti;
public static CentralViewBaseCommands GoToEti => _goToEti ?? new 
    CentralViewBaseCommands(GoToExternalWindow);
    private static void GoToExternalWindow(object obj)
    {
        if (obj is GridContextMenuInfo)
        {
            object record = (obj as GridRecordContextMenuInfo)?.Record;
            CentralDataGridModel mwSfDgModel = (CentralDataGridModel)record;
            if (mwSfDgModel != null)
            {
                //TODO
            }
        }
    }

CentralViewBaseCommands :

public class CentralViewBaseCommands : ICommand
{
    private Action<object> _execute;
    private Predicate<object> _canExecute;
    public CentralViewBaseCommands(Action<object> execute) : this(execute, null)
    {
        _execute = execute;
    }
    public CentralViewBaseCommands(Action<object> execute,Predicate<object> canExecute)
    {
        if (execute == null)
        throw new ArgumentNullException(nameof(execute));
    _execute = execute;
    _canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
    return _canExecute?.Invoke(parameter) ?? true;
}
public void Execute(object parameter)
{
    _execute(parameter);
}
    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }
}

菜单项的代码如下(位于用户控件中):

<MenuItem Header="Исправление ошибок">
    <MenuItem Command="{Binding Source={x:Static Member=command:CentralViewCommands.GoToEti}}"
              CommandParameter="{Binding}"
              Header="Удаление ошибочно внесенной техники">
        <MenuItem.Icon>
            <Viewbox>
                <Grid>
                    <Grid Width="32"
                          Height="32"
                          Visibility="Collapsed" />
                    <Path Width="26"
                          Height="26"
                          Margin="0,0,0,0"
                          Fill="#FFFF0000"
                          RenderTransformOrigin="0.5,0.5"
                          Stretch="Uniform">
                        <Path.RenderTransform>
                            <TransformGroup>
                                <TransformGroup.Children>
                                    <RotateTransform Angle="0" />
                                    <ScaleTransform ScaleX="1" ScaleY="1" />
                                </TransformGroup.Children>
                            </TransformGroup>
                        </Path.RenderTransform>
                    </Path>
                </Grid>
            </Viewbox>
        </MenuItem.Icon>
    </MenuItem>
</MenuItem>

所以,问题就出在了跟上。当我单击菜单项命令按预期执行时,但是如果我将命令绑定到命令UserControl ViewModel (DelegateCommand<>) Command="{Binding CommandInViewModel}"则没有任何反应,命令未执行。有人可以解释我为什么吗?

菜单项命令绑定到位于“窗口”中的“用户控件”中的视图模型

我认为这是因为这一行:

public static CentralViewBaseCommands GoToEti => _goToEti ?? new CentralViewBaseCommands(GoToExternalWindow);

总是生成CentralViewBaseCommands的新实例。

试试这个:

public static CentralViewBaseCommands GoToEti { get; } = new CentralViewBaseCommands(GoToExternalWindow);

并删除_goToEti.

希望对您有所帮助!