使用 MVVMLight EventToCommand 和页面加载事件传递 CommandParameter?

发布于 2024-12-11 08:22:45 字数 1029 浏览 0 评论 0原文

EventToCommand 无法在 Load 事件上传递命令参数

当附加到页面或用户控件的 Load 事件时,EventToCommand 成功调用 ViewModel 中的处理程序,但不传递 CommandParameter。但是,相同的 XAML 附加到另一个事件,例如按钮单击,命令处理程序接收数据绑定数据作为其参数。 Xaml:

<命令:EventToCommand x:名称=“etcLoad” 命令=“{绑定加载命令}” CommandParameter="{绑定目标, ElementName=Control}" />

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 技术交流群。

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

发布评论

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

评论(1

看透却不说透 2024-12-18 08:22:45

使用 PassEventArgsToCommand 属性指示应将事件参数传递给命令。因此,在您的 XAML 中,您应该使用:

<i:EventTrigger EventName="Loaded" 
                SourceObject="{Binding ElementName=Control}"> 
    <Command:EventToCommand x:Name="etcLoad" 
                            Command="{Binding LoadCommand}" 
                            PassEventArgsToCommand="True" /> 
</i:EventTrigger>

编辑

某些事件会在用户交互发生之前触发。在这种情况下通常采取的方法是从代码隐藏中调用命令。在这篇文章中你可以看到这个概念,你显然必须将其更改为不过,加载事件和您的需求、概念和原因是相同的。

Use the PassEventArgsToCommand property to indicate that the event args should be passed to the command. In your XAML you should, therefore, use:

<i:EventTrigger EventName="Loaded" 
                SourceObject="{Binding ElementName=Control}"> 
    <Command:EventToCommand x:Name="etcLoad" 
                            Command="{Binding LoadCommand}" 
                            PassEventArgsToCommand="True" /> 
</i:EventTrigger>

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.

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