从全局样式中定义的Button执行(静态)DelegateCommand [英] Execute (static) DelegateCommand from Button defined inside global style

查看:145
本文介绍了从全局样式中定义的Button执行(静态)DelegateCommand的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题可能不太清楚,所以这里是我的情况和我要实现的目标的简短摘要:

我有一个类,其中包含为解决方案定义的所有样式,这是一个全局类,供多个解决方案使用.

在这里,我有一个控件模板,该模板除其他元素外还有一个按钮.我对这个按钮特别感兴趣,因为我需要为此按钮定义一个命令,以便当用户单击它时,Presenter/ViewModel中的方法就会运行.

使用静态全局CompositeCommand I(认为),我可以执行操作(注册本地DelegateCommand并使用CompositeCommand作为对Button命令的绑定),但是我只需要执行MY命令,如果有人 在此CompositeCommand中注册命令,它们的命令也将被执行.

只能使用DelegateCommands来完成此操作,例如将本地Execute/CanExecute方法分配给全局DelegateCommand方法吗?

有什么好方法可以做到这一点,而不必在本地覆盖样式或在本地分配EventSetters?

 

预先感谢.

解决方案

您可以使用ICommand和委托实现

 

当您单击按钮时,说"RemoveMemberCommand"是Buttion的命令参数

 

它将调用VM中的RemoveMemberCommand

_removeMemberCommand 是一个委托命令,它知道要执行什么方法才能调用.

现在它将执行deleteMembersList方法并执行相应的操作.

 

 

公共ICommand RemoveMemberCommand

         {

            获取

             {

                 if(_removeMemberCommand == null)

                 {

 

                     _removeMemberCommand =新的SNADelegateCommand(deleteMembersList);

 

                }

                返回_removeMemberCommand;

            }

        }

 

 

您必须使用ICommand 来实现SNADelegate

 

始终最好在基本方法中实现SNADelegate,因为您在所有VM上都使用了它.

 

如果您需要更多信息,请告诉我



The title might not be very clear so here is a brief summary of my situation and what I want to achieve:

I have a class which contains all the styles defined for the solution, this is a global class that is used by several solutions.

In there I have a template for a control, which, among other elements, has a button. I'm particularly interested in this button because I need to define a command for this button so that when the user clicks it, a method in my Presenter/ViewModel runs.

Using a static global CompositeCommand I (think) I can perform the action (registering a local DelegateCommand and using the CompositeCommand as Binding to the Command of the Button) but the thing is I only need to execute MY command and if somebody else registers commands in this CompositeCommand theirs will also be executed.

Can this be done only with DelegateCommands, like assigning the local Execute/CanExecute methods to the global DelegateCommand methods?

Is there any nice way to do this, without overriding locally the style or assigning EventSetters locally?

 

Thanks in advance.

解决方案

You can implement using the ICommand and the delegate 

 

Lets say RemoveMemberCommand is the command parameter for the Buttion when you click on the button 

 

it will call the RemoveMemberCommand in the VM 

_removeMemberCommand  is a delegate command and it knows what to do what method to call.

now it will execute the deleteMembersList method and do the respective operation.

 

 

public ICommand RemoveMemberCommand

        {

            get

            {

                if (_removeMemberCommand == null)

                {

 

                    _removeMemberCommand = new SNADelegateCommand(deleteMembersList);

 

                }

                return _removeMemberCommand;

            }

        }

 

 

you have to implement SNADelegate using ICommand 

 

Always It's best to implement the SNADelegate in the base method because you use it on all your VM.

 

Please let me know if you need more info



这篇关于从全局样式中定义的Button执行(静态)DelegateCommand的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆