使用 MVVMLight EventToCommand 和页面加载事件传递 CommandParameter?
EventToCommand 无法在 Load 事件上传递命令参数
当附加到页面或用户控件的 Load 事件时,EventToCommand 成功调用 ViewModel 中的处理程序,但不传递 CommandParameter。但是,相同的 XAML 附加到另一个事件,例如按钮单击,命令处理程序接收数据绑定数据作为其参数。 Xaml:
Target 是 View 上的字符串 DP。
VM 代码:
internal void Load(string p_Param)
{
this.Initialise();
}
public RelayCommand<string> LoadCommand { get; private set; }
并且命令是这样分配的:
this.LoadCommand = new RelayCommand<string>(this.Load);
我几乎可以肯定问题在于绑定晚于分配给目标 DP 或类似的东西。我有兴趣尽快找到解决方案,或者以其他方式将字符串从 View 中取出并放入 ViewModel 中,其中该字符串是从 OnNavigateTo 覆盖分配的。目标是根据通过 URI 提供的查询属性(即“/Views/DisplayTabDetails?Tab=Tab1”或类似属性)提供选项卡选择。
EventToCommand fails to pass Command Parameter on Load Event
When attached to the Load event of the page or user control the EventToCommand successfully calls the handler in the ViewModel but does not pass the CommandParameter. However, the same XAML is attached to another event, button click for example, the Command handler receives the databound data as its parameter.
Xaml:
<i:EventTrigger EventName="Loaded" SourceObject="{Binding ElementName=Control}">
<Command:EventToCommand x:Name="etcLoad"
Command="{Binding LoadCommand}"
CommandParameter="{Binding Target, ElementName=Control}" />
</i:EventTrigger>
Target is a string DP on the View.
VM Code:
internal void Load(string p_Param)
{
this.Initialise();
}
public RelayCommand<string> LoadCommand { get; private set; }
and the Command is assigned so:
this.LoadCommand = new RelayCommand<string>(this.Load);
I am almost certain that the problem lies with the binding being done later than the assignment to the Target DP or something similar. I am interested in finding a solution for this ASAP or some other way that I might get a string out of the View and into the ViewModel where the string is assigned from the OnNavigateTo override. The goal is to provide the selection of a tab based on a query property supplied via the URI i.e. "/Views/DisplayTabDetails?Tab=Tab1" or similar.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
PassEventArgsToCommand
属性指示应将事件参数传递给命令。因此,在您的 XAML 中,您应该使用:编辑
某些事件会在用户交互发生之前触发。在这种情况下通常采取的方法是从代码隐藏中调用命令。在这篇文章中你可以看到这个概念,你显然必须将其更改为不过,加载事件和您的需求、概念和原因是相同的。
Use the
PassEventArgsToCommand
property to indicate that the event args should be passed to the command. In your XAML you should, therefore, use:Edit
Some events fire before the user interaction can take place. The approach normally taken in this case is to call your command from code behind. In this post you can see the concept, you obviously will have to change it to the loaded event and your needs, the concept and the reason for it are the same, though.