使用InvokeDelegateCommandAction的事件触发的CommandParameter

发布于 2024-11-16 09:58:42 字数 2596 浏览 2 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(2

北城孤痞 2024-11-23 09:58:42

我用 ListBox 代替,但我得到了同样的结果。以下内容很好,因为它传递 CommandParameter 而不是调用参数。那么为什么 CommandParameter null 呢?

protected override void Invoke( object parameter ) {
    this.InvokeParameter = parameter;

    if ( this.AssociatedObject != null ) {
        ICommand command = this.ResolveCommand();
        if ( ( command != null ) && command.CanExecute( this.CommandParameter ) ) {
            command.Execute( this.CommandParameter );
        }
    }
}

CommandParameter 似乎无法正常工作,因为您的绑定将其设置为 null{RelativeSource Self} 解析为 InvokeDelegateCommandAction,并且没有 SelectedItems 属性。相反,请使用此绑定:

CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBox}}, Path=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 passes CommandParameter instead of the invoke parameter. So why is CommandParameter null?

protected override void Invoke( object parameter ) {
    this.InvokeParameter = parameter;

    if ( this.AssociatedObject != null ) {
        ICommand command = this.ResolveCommand();
        if ( ( command != null ) && command.CanExecute( this.CommandParameter ) ) {
            command.Execute( this.CommandParameter );
        }
    }
}

CommandParameter doesn't seem to be working appropriately because your binding is setting it to null. {RelativeSource Self} resolves to an InvokeDelegateCommandAction, and that doesn't have a SelectedItems property. Instead, use this binding:

CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBox}}, Path=SelectedItems}"

Then CommandParameter will pass in a SelectedItemCollection from the ListBox.

There is one other issue that you'll quickly discover. DoSelectedActiveItemsChanged()'s param will be an instance of SelectedItemCollection, not List<Object>.

黎夕旧梦 2024-11-23 09:58:42

我在乔尔的观察的帮助下解决了这个问题......对于那些可能感兴趣的人,这里是如何解决的。尽管显然我正确地认为乔尔给出了正确的答案,但将此信息作为答案而不是对问题的编辑似乎是正确的。

我发现了通用的 DelegateCommand,因此摆脱了 DelegateCommandWithParameter

public ICommand SelectedObjectsChangedCommand 
{
    get
    {
        return selectedObjectsChangedCommand ??
            (selectedObjectsChangedCommand = new DelegateCommand<MyType>(DoSelectedObjectsChangedCommand , CanDoSelectedObjectsChangedCommand ));
    }
}

现在,随着 SelectionChangedEventArgs 的返回,“Do”(执行方法)...

public void DoSelectedObjectsChangedCommand(object param)
{
    if (param != null && param is SelectionChangedEventArgs)
    {
        foreach (MyType object in ((SelectionChangedEventArgs)param).AddedItems.Cast<MyType>().ToList())
        {
            selectedObjects.Add(object);
        }
        foreach (MyType object in ((SelectionChangedEventArgs)param).RemovedItems.Cast<MyType>().ToList())
        {
            selectedObjects.Remove(object);
        }
        UpdateAllCanDos();
    }
} 

连同我的其余业务逻辑一起,它带来了非常流畅和直观的用户体验。非常感谢所有的回答者。我使用 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

public ICommand SelectedObjectsChangedCommand 
{
    get
    {
        return selectedObjectsChangedCommand ??
            (selectedObjectsChangedCommand = new DelegateCommand<MyType>(DoSelectedObjectsChangedCommand , CanDoSelectedObjectsChangedCommand ));
    }
}

The 'Do' (Execute method) now with the return of the SelectionChangedEventArgs...

public void DoSelectedObjectsChangedCommand(object param)
{
    if (param != null && param is SelectionChangedEventArgs)
    {
        foreach (MyType object in ((SelectionChangedEventArgs)param).AddedItems.Cast<MyType>().ToList())
        {
            selectedObjects.Add(object);
        }
        foreach (MyType object in ((SelectionChangedEventArgs)param).RemovedItems.Cast<MyType>().ToList())
        {
            selectedObjects.Remove(object);
        }
        UpdateAllCanDos();
    }
} 

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.

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