传递命令参数

发布于 2025-01-03 05:29:05 字数 2384 浏览 0 评论 0原文

我正在尝试用我的命令传递命令参数。我有一般工作的命令,但传递参数似乎对我来说不太好。

我正在尝试从 XAML 中的分层数据传递 UserName 属性。我在这里做错了什么。

我尝试使用命令语句进行编译时收到错误:

无法从“lambda 表达式”转换为“System.Action”

<HierarchicalDataTemplate 
    DataType="{x:Type viewModel:UsersViewModel}" 
    ItemsSource="{Binding Children}">
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding UserName}">
            <TextBlock.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Edit" Command="{Binding EditCommand}" CommandParameter="{Binding UserName}"/>
                        <MenuItem Header="Delete"/>
                    </ContextMenu>
                </TextBlock.ContextMenu>
        </TextBlock>
    </StackPanel>
</HierarchicalDataTemplate>
private RelayCommand _editCommand;
    public ICommand EditCommand
    {
        get
        {
            if (_editCommand== null)
            {
                _editCommand= new RelayCommand(param => this.LoadUser(object parameter));
            }
            return _editCommand;
        }
    }

    public void LoadUser(object username)
    {

    } 

RelayCommand 类

public class RelayCommand : ICommand
{
    #region Fields

    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;

    #endregion // Fields

    #region Constructors

    public RelayCommand(Action<object> execute)
        : this(execute, null)
    {
    }

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");

        _execute = execute;
        _canExecute = canExecute;
    }
    #endregion // Constructors

    #region ICommand Members

    [DebuggerStepThrough]
    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public void Execute(object parameter)
    {
        _execute(parameter);
    }
    #endregion
}

感谢您的帮助!

I'm trying to pass a command parameter with my command. I have commands in general working but passing a parameter doesn't seem to be going to well for me.

I'm trying to pass the UserName Property from the Hierarchical Data in my XAML. What am I doing wrong here.

I recieve and error trying to compile with the commands statement:

cannot convert from 'lambda expression' to 'System.Action'

<HierarchicalDataTemplate 
    DataType="{x:Type viewModel:UsersViewModel}" 
    ItemsSource="{Binding Children}">
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding UserName}">
            <TextBlock.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Edit" Command="{Binding EditCommand}" CommandParameter="{Binding UserName}"/>
                        <MenuItem Header="Delete"/>
                    </ContextMenu>
                </TextBlock.ContextMenu>
        </TextBlock>
    </StackPanel>
</HierarchicalDataTemplate>
private RelayCommand _editCommand;
    public ICommand EditCommand
    {
        get
        {
            if (_editCommand== null)
            {
                _editCommand= new RelayCommand(param => this.LoadUser(object parameter));
            }
            return _editCommand;
        }
    }

    public void LoadUser(object username)
    {

    } 

RelayCommand Class

public class RelayCommand : ICommand
{
    #region Fields

    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;

    #endregion // Fields

    #region Constructors

    public RelayCommand(Action<object> execute)
        : this(execute, null)
    {
    }

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");

        _execute = execute;
        _canExecute = canExecute;
    }
    #endregion // Constructors

    #region ICommand Members

    [DebuggerStepThrough]
    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public void Execute(object parameter)
    {
        _execute(parameter);
    }
    #endregion
}

Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜嗑 2025-01-10 05:29:05
new RelayCommand(param => this.LoadUser(object parameter));

这不应该是:

new RelayCommand(param => this.LoadUser(param));
new RelayCommand(param => this.LoadUser(object parameter));

Shouldn't this be:

new RelayCommand(param => this.LoadUser(param));
染火枫林 2025-01-10 05:29:05

您不应该调用该方法,而应该将其作为参数传递。只需将 new RelayCommand(param => this.LoadUser(objectparameter)); 替换为 new RelayCommand(this.LoadUser);

此处类似问题:
RelayCommand lambda 语法问题

You should not invoke the method, you should pass it as a parameter. Just replace new RelayCommand(param => this.LoadUser(object parameter)); for new RelayCommand(this.LoadUser);

Similar question here:
RelayCommand lambda syntax problem

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文