簡體   English   中英

Menuitem命令綁定到位於Window的UserControl中的ViewModel

[英]Menuitem command binding to ViewModel in UserControl located in Window

我有一個帶有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="Исправление ошибок">
    <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}"命令ViewModel (DelegateCommand<>) Command="{Binding CommandInViewModel}"什么也沒有發生,並且命令沒有執行。 有人可以解釋我為什么嗎?

我認為是因為這一行:

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

一直在生成CentralViewBaseCommands的新實例。

嘗試以下方法:

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

並刪除_goToEti

希望能幫助到你!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM