如何将变量作为 CommandParameter 传递 [英] How do I pass a variable as a CommandParameter
问题描述
我正在尝试将 ViewModel 中的变量作为参数发送给命令.命令如下所示:
I'm trying to send a variable from the ViewModel as a parameter to a command. The command looks like this:
public class EditPersonCommand : ICommand
{
private bool _CanExecute = false;
public bool CanExecute(object parameter)
{
PersonModel p = parameter as PersonModel;
CanExecuteProperty = (p != null) && (p.Age > 0);
return CanExecuteProperty;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter) { }
private bool CanExecuteProperty
{
get { return _CanExecute; }
set
{
if (_CanExecute != value)
{
_CanExecute = value;
EventHandler can_execute = CanExecuteChanged;
if (can_execute != null)
{
can_execute.Invoke(this, EventArgs.Empty);
}
}
}
}
}
ViewModel 如下所示:
The ViewModel looks like this:
public class PersonViewModel : ViewModelBase
{
private PersonModel _PersonModel;
private EditPersonCommand _EditPersonCommand;
///<remarks>
/// must use the parameterless constructor to satisfy <Window.Resources>
///</remarks>
public PersonViewModel()
: this(new PersonModel())
{
}
public PersonViewModel(PersonModel personModel)
{
_PersonModel = personModel;
}
public ICommand EditPersonCommand
{
get
{
if (_EditPersonCommand == null)
{
_EditPersonCommand = new EditPersonCommand();
}
return _EditPersonCommand;
}
}
}
xaml 如下所示:
<Button Content="Edit" HorizontalAlignment="Right" Height="20" Width="80"
Command="{Binding EditPersonCommand}"
CommandParameter="{Binding _PersonModel}" />
我尝试在 ViewModel 中创建一个属性,而不是使用私有局部变量名称,但这也不起作用.object 参数
在对 CanExecute
的调用中总是显示 null
并且按钮永远不会启用.如果我将 CommandParameter
值更改为 Hello
,那么我会在对 CanExecute
的调用中收到 Hello
,所以我我不确定为什么变量不起作用.任何帮助将不胜感激.
I've tried creating a property in the ViewModel instead of using the private local variable name, but that didnt work either. The object parameter
always shows null
in the call to CanExecute
and the button is never enabled. If I change the CommandParameter
value to Hello
, then I receive Hello
in the call to CanExecute
, so I'm not sure why the variable doesnt work. Any help would be appreciated.
更新:我还尝试为模型创建一个公共属性(我真的不想公开模型,但只是尝试看看它是否有效,但没有).
Update: I've also tried making a public property to the model (which I dont really want to expose the model, but just tried it to see if it works, but it doesnt).
// Added this to the ViewModel
public PersonModel PersonModelProp
{
get
{
return _PersonModel;
}
set
{
_PersonModel = value;
OnPropertyChanged("PersonModelProp");
}
}
并将 xaml 更改为:
And changed the xaml to this:
<Button Content="Edit" HorizontalAlignment="Right" Height="20" Width="80"
Command="{Binding EditPersonCommand}"
CommandParameter="{Binding PersonModelProp}" />
但仍然没有运气.ViewModel 确实实现了 INotifyPropertyChanged
But still no luck. The ViewModel does implement INotifyPropertyChanged
推荐答案
CommandParameter 是否始终为空,还是仅在第一次执行时检查?
Is the CommandParameter always null or are you only checking the first time it is being executed?
在这种情况下,您声明属性的顺序似乎很重要,因为设置 Command 属性会导致 CanExecute 在设置 CommandParameter 之前立即触发.
It appears that the order in which you declare your properties matters in this case since setting the Command property causes the CanExecute to fire immediately before the CommandParameter has been set.
尝试将 CommandParameter 属性移到 Command 属性之前:
Try moving the CommandParameter property before the Command property:
<Button Content="Edit" HorizontalAlignment="Right" Height="20" Width="80"
CommandParameter="{Binding PersonModelProp}"
Command="{Binding EditPersonCommand}" />
编辑
为确保正确引发您的事件,您应该在 PersonModelProp
值更改时引发 CanExecuteChanged 事件.
To ensure that your events are being raised properly you should raise the CanExecuteChanged event when the PersonModelProp
value changes.
命令:
public class EditPersonCommand : ICommand
{
public bool CanExecute(object parameter)
{
PersonModel p = parameter as PersonModel;
return p != null && p.Age > 0;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
//command implementation
}
public void RaiseCanExecuteChanged()
{
var handler = CanExecuteChanged;
if(handler != null)
{
handler(this, EventArgs.Empty);
}
}
}
和视图模型:
public class PersonViewModel : ViewModelBase
{
private PersonModel _PersonModel;
private EditPersonCommand _EditPersonCommand;
///<remarks>
/// must use the parameterless constructor to satisfy <Window.Resources>
///</remarks>
public PersonViewModel()
: this(new PersonModel())
{
_EditPersonCommand = new EditPersonCommand();
}
public PersonViewModel(PersonModel personModel)
{
_PersonModel = personModel;
}
public ICommand EditPersonCommand
{
get
{
return _EditPersonCommand;
}
}
public PersonModel PersonModelProp
{
get
{
return _PersonModel;
}
set
{
_PersonModel = value;
OnPropertyChanged("PersonModelProp");
EditPersonCommand.RaiseCanExecuteChanged();
}
}
}
这篇关于如何将变量作为 CommandParameter 传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!