Menuitem 命令绑定到位于 Window 中的 UserControl 中的 ViewModel

问题描述 投票:0回答:1

我有一个带有

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 的 UserControl。另外,我有一个简单的类,它实现了 ICommand 接口和存储所有命令的静态 Command 类:

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 的代码如下(位于 UserControl 中):

<MenuItem Header="Errors">
    <MenuItem Command="{Binding Source={x:Static Member=command:CentralViewCommands.GoToEti}}"
              CommandParameter="{Binding}"
              Header="Delete">
        <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}"
中的命令,则什么也不会发生,命令也不会执行。 有人可以解释一下为什么吗?

c# wpf
1个回答
1
投票

我认为这是因为这行:

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

始终生成

CentralViewBaseCommands
的新实例。

试试这个:

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

并删除

_goToEti

希望有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.