传递命令参数
我正在尝试用我的命令传递命令参数。我有一般工作的命令,但传递参数似乎对我来说不太好。
我正在尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不应该是:
Shouldn't this be:
您不应该调用该方法,而应该将其作为参数传递。只需将
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));
fornew RelayCommand(this.LoadUser);
Similar question here:
RelayCommand lambda syntax problem