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

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

            public ICommand CMD
            {
                get
                {
                    return new DelegateCommand<object[]>(x =>
                    {
                        var obj0=x[0].ToString();
                        var obj1=Convert.ToInt32(x[1]);
                        //.................
                    });
                }
            }

          //调用处:
          {
             .........
             v.CMD.Execute(new object[]{"1111",456});//v为该CMD所在的vm的实例
         }

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

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

先看看应用:

<Button Command="{Binding CMD}">
   <Button.CommandParameter>
      <sw:MultiDelegateObjects>
          <sw:DelegateObject Key="s1" Value="{Binding Tag, ElementName=t1}"/>
          <sw:DelegateObject Key="s2" Value="888"/>
      </sw:MultiDelegateObjects>
   </Button.CommandParameter>
</Button>

vm:
public ICommand CMD
{
   get
   {
      return new DelegateCommand<MultiDelegateObjects>(x =>
      {
          //使用key或索引来获取集合的元素。key不区分大小写.
          //使用key可以不要考虑在xaml中的位置。
          var obj0=x["s1"] as Page;//或x.GetValue<Page>("s1");
          var obj1=x["s2"].ToString();
          var obj2=x[1].ToString();//obj1==obj2;
          //.................
      });
   }
}

 

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  
 }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值