微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

mvvm---如何在xaml里,把多个参数传入到command

     mvvm模式里command经常写在vm中。而command只能传入一个对象作为执行时的参数,若要传入多个参数,在.cs文件(即调用vm的command)中,只需要把多个参数加到一个集合里,传入command时就把集合当单参数对象传入就行了。

     如:

 

     但如果在xaml中用到如blend的InvokeCommandAction进行command的绑定,又如何在xaml中进行传入多参数??

     方法有许多种。小弟不才,自己开发了2个类来解决这问题。先说明一下,此方法只使用与silverlight4或以上版本。

先看看应用:

 

关于DelegateCommand的实现:

using System;using System.Windows.Input;namespace System.Windows.Input{    public class DelegateCommand<T> : ICommand    {        public DelegateCommand() : this(null,null) { }        public DelegateCommand(Action<T> executeMethod) : this(executeMethod,null) { }        public DelegateCommand(Action<T> executeMethod,Func<T,bool> canExecuteMethod)        {            TargetExecuteMethod = executeMethod;            TargetCanExecuteMethod = canExecuteMethod;        }        public Action<T> TargetExecuteMethod { get; set; }        public Func<T,bool> TargetCanExecuteMethod { get; set; }        public void OnCanExecuteChanged()        {            this.CanExecuteChanged(this,EventArgs.Empty);        }        public void Execute(T parameter)        {            if (TargetExecuteMethod != null) TargetExecuteMethod(parameter);        }        public bool CanExecute(T parameter)        {            if (TargetCanExecuteMethod != null)                return TargetCanExecuteMethod(parameter);            if (TargetExecuteMethod != null)                return true;            return false;        }        #region ICommand        bool ICommand.CanExecute(object parameter)        {            return this.CanExecute((T)parameter);        }        void ICommand.Execute(object parameter)        {            this.Execute((T)parameter);        }        public event EventHandler CanExecuteChanged;        #endregion    } }


 

欢迎各大网友来吐槽。

下载地址:http://download.csdn.net/source/2979146

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐