简体   繁体   中英

Menuitem command binding to ViewModel in UserControl located in Window

I have a Window with 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 is UserControl with DataGrid and DataGrid ContextMenu. Also, i have simple class witch implements ICommand interface and static Command class where stored all commands:

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; }
    }
}

Code of MenuItem is following (Which located in UserControl):

<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>

So, the problem is following. When i click on menu item command executed as i expect, but if i bind command to command in UserControl ViewModel (DelegateCommand<>) Command="{Binding CommandInViewModel}" nothing is happening and command not executing. Can someone explain me why?

I think it's because this line:

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

is always generating a new instance of CentralViewBaseCommands .

Try this instead:

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

And remove _goToEti .

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM