使用InvokeDelegateCommandAction的事件触发的CommandParameter
我正在使用 AlexeyZakharov 的weblog 基于一些人的建议,这是从 EventTrigger 将参数从 View 发送回 ViewModel 的最佳方式。
这就是我所拥有的。
在视图中(具体来说是 DataGrid):
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged" >
<cmnwin:InvokeDelegateCommandAction
Command="{Binding SelectedExcludedItemChangedCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource self}, Path=SelectedItems}" />
</i:EventTrigger>
</i:Interaction.Triggers>
在 ViewModel 中:
public DelegateCommandWithParameter SelectedActiveItemChangedCommand
{
get
{
return selectedActiveItemChangedCommand ??
(selectedActiveItemChangedCommand = new DelegateCommandWithParameter(DoSelectedActiveItemsChanged, CanDoSelectedActiveItemsChanged));
}
}
public bool CanDoSelectedActiveItemsChanged(object param)
{
return true;
}
public void DoSelectedActiveItemsChanged(object param)
{
if (param != null && param is List<Object>)
{
var List = param as List<Object>;
MyLocalField = List;
}
}
新型 DelegateCommand 允许我将对象作为参数传递:
public class DelegateCommandWithParameter : ICommand
{
#region Private Fields
private Func<object, bool> canExecute;
private Action<object> executeAction;
private bool canExecuteCache;
#endregion
#region Constructor
public DelegateCommandWithParameter(Action<object> executeAction, Func<object, bool> canExecute)
{
this.executeAction = executeAction;
this.canExecute = canExecute;
}
#endregion
#region ICommand Members
public bool CanExecute(object parameter)
{
bool temp = canExecute(parameter);
if (canExecuteCache != temp)
{
canExecuteCache = temp;
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, new EventArgs());
}
}
return canExecuteCache;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
executeAction(parameter);
}
#endregion
}
每当我的代码到达 DoSelectedActiveItemsChanged 时,参数始终为 NULL....我是一个这里完全是傻瓜吗? CommandParamter 在哪里链接到命令参数?又名,为什么视图不将任何内容传递回命令?请帮忙。
I am using the class InvokeDelegateCommandAction from AlexeyZakharov's weblog on the basis of some advice from guys that this is the best way to send back a parameter from a View to a ViewModel from an EventTrigger.
Here's what I have.
In the View (a DataGrid to be specific):
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged" >
<cmnwin:InvokeDelegateCommandAction
Command="{Binding SelectedExcludedItemChangedCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource self}, Path=SelectedItems}" />
</i:EventTrigger>
</i:Interaction.Triggers>
In the ViewModel:
public DelegateCommandWithParameter SelectedActiveItemChangedCommand
{
get
{
return selectedActiveItemChangedCommand ??
(selectedActiveItemChangedCommand = new DelegateCommandWithParameter(DoSelectedActiveItemsChanged, CanDoSelectedActiveItemsChanged));
}
}
public bool CanDoSelectedActiveItemsChanged(object param)
{
return true;
}
public void DoSelectedActiveItemsChanged(object param)
{
if (param != null && param is List<Object>)
{
var List = param as List<Object>;
MyLocalField = List;
}
}
The new kind of DelegateCommand that allows me to pass objects as args:
public class DelegateCommandWithParameter : ICommand
{
#region Private Fields
private Func<object, bool> canExecute;
private Action<object> executeAction;
private bool canExecuteCache;
#endregion
#region Constructor
public DelegateCommandWithParameter(Action<object> executeAction, Func<object, bool> canExecute)
{
this.executeAction = executeAction;
this.canExecute = canExecute;
}
#endregion
#region ICommand Members
public bool CanExecute(object parameter)
{
bool temp = canExecute(parameter);
if (canExecuteCache != temp)
{
canExecuteCache = temp;
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, new EventArgs());
}
}
return canExecuteCache;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
executeAction(parameter);
}
#endregion
}
Whenever my code gets to the DoSelectedActiveItemsChanged, the arg is always NULL.... am I being a complete doofus here? Where does the CommandParamter get linked to the command args? AKA, why does the View pass nothing back to the command? Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我用
ListBox
代替,但我得到了同样的结果。以下内容很好,因为它传递 CommandParameter 而不是调用参数。那么为什么CommandParameter
null
呢?CommandParameter
似乎无法正常工作,因为您的绑定将其设置为null
。{RelativeSource Self}
解析为InvokeDelegateCommandAction
,并且没有SelectedItems
属性。相反,请使用此绑定:然后
CommandParameter
将从ListBox
传入SelectedItemCollection
。您很快就会发现另一个问题。
DoSelectedActiveItemsChanged()
的param
将是SelectedItemCollection
的实例,而不是List
I did it with a
ListBox
instead, but I got the same thing. The following is fine, as it passesCommandParameter
instead of the invoke parameter. So why isCommandParameter
null
?CommandParameter
doesn't seem to be working appropriately because your binding is setting it tonull
.{RelativeSource Self}
resolves to anInvokeDelegateCommandAction
, and that doesn't have aSelectedItems
property. Instead, use this binding:Then
CommandParameter
will pass in aSelectedItemCollection
from theListBox
.There is one other issue that you'll quickly discover.
DoSelectedActiveItemsChanged()
'sparam
will be an instance ofSelectedItemCollection
, notList<Object>
.我在乔尔的观察的帮助下解决了这个问题......对于那些可能感兴趣的人,这里是如何解决的。尽管显然我正确地认为乔尔给出了正确的答案,但将此信息作为答案而不是对问题的编辑似乎是正确的。
我发现了通用的 DelegateCommand,因此摆脱了 DelegateCommandWithParameter
现在,随着 SelectionChangedEventArgs 的返回,“Do”(执行方法)...
连同我的其余业务逻辑一起,它带来了非常流畅和直观的用户体验。非常感谢所有的回答者。我使用 NHibernate 进行 WPF 和 MVVM 工作已经不只一个月了,我不得不承认 SO 社区正在以尽可能最好、最丰富的方式帮助我克服学习曲线。
I have solved the problem with the aid of Joel's observations... For those that may be interested, here's how. Although obviously I have credited Joel rightfully with the correct answer, it seems right to put this info as a answer rather than an edit to the question.
I discovered the generic DelegateCommand, so got rid of the DelegateCommandWithParameter
The 'Do' (Execute method) now with the return of the SelectionChangedEventArgs...
Along with the rest of my business logic it makes for a very smooth and intuitive UX. Many thanks to all the answer-ers. I have been doing WPF and MVVM with NHibernate not for only a month and I can't help but acknowledge that the SO community are getting me over the learning curves in the best and most enriching ways possible.