【问题标题】:Menuitem command binding to ViewModel in UserControl located in WindowMenuitem 命令绑定到位于 Window 中的 UserControl 中的 ViewModel
【发布时间】:2016-11-17 08:13:25
【问题描述】:

我有一个带有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&lt;&gt;) Command="{Binding CommandInViewModel}" 中的命令,则没有任何事情发生并且命令未执行。 谁能解释一下为什么?

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    我认为是因为这行:

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

    总是生成CentralViewBaseCommands 的新实例。

    试试这个:

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

    并删除_goToEti

    希望对你有帮助!

    【讨论】:

    • 您的更正版本应该删除{ get; };这不会编译。
    • 感谢您的回答。这是真的,但是,我想在 ViewModel 类中使用命令( CentralView 的 DataContext )。像这样: private DelegateCommand _goToEti 并在 ViewModel 类中使用。但是,在 ViewModel 类中,该命令未执行
    • 您使用的是哪个版本的 Visual Studio,{ get;不会编译? geekswithblogs.net/WinAZ/archive/2015/06/30/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    • 2011-06-22
    • 1970-01-01
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多