MVVM --- Command 绑定

(一)简单绑定

    

网上有很多关于MVVM的Command绑定,我这里介绍一种借助Prism的非常方便的绑定方式。 

1.项目中添加Prism.dll引用

2.创建ViewModel,定义ICommand命令和委托方法,注意DelegateCommand是Prism中的类

[csharp]  view plain copy
  1. public class Window1ViewModel {  
  2.   
  3.     public ICommand ButtonCommand {  
  4.         get {  
  5.             return new DelegateCommand(() => {  
  6.                 MessageBox.Show("Button");  
  7.             });  
  8.         }  
  9.     }  
  10. }  

 

3.xaml中绑定,先指定DataContext,然后在Button的Command中指定绑定ButtonCommand命令

[html]  view plain copy
  1. <Window x:Class="WpfApplication1.Window1"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         xmlns:vm="clr-namespace:WpfApplication1"  
  5.         Title="Window1" Height="193" Width="190">  
  6.     <Window.DataContext>  
  7.         <vm:Window1ViewModel />  
  8.     </Window.DataContext>  
  9.     <Grid>  
  10.         <Button Content="Button" Height="33" HorizontalAlignment="Left" Margin="34,20,0,0" Name="button1" VerticalAlignment="Top" Width="109"  
  11.         Command="{Binding ButtonCommand}"/>  
  12.     </Grid>  
  13. </Window>  


点击按钮后,显示了对话框。成功!



(二)传递Command参数

我们如果需要在Command中传递参数,实现也很简单。DelegateCommand还有一个DelegateCommand<T>版本,可以传递一个T类型的参数。

1.View的Button绑定,其中CommandParameter定义了一个“20”的参数

[html]  view plain copy
  1. <Window x:Class="WpfApplication1.Window1"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         xmlns:vm="clr-namespace:WpfApplication1"  
  5.         Title="Window1" Height="193" Width="190">  
  6.     <Window.DataContext>  
  7.         <vm:Window1ViewModel />  
  8.     </Window.DataContext>  
  9.     <Grid>  
  10.         <Button Content="Button" Height="33" HorizontalAlignment="Left" Margin="34,20,0,0" Name="button1" VerticalAlignment="Top" Width="109"  
  11.                 Command="{Binding ButtonCommand}"  
  12.                 CommandParameter="20"/>  
  13.     </Grid>  
  14. </Window>  


2.ViewModel定义命令,注意委托参数

[csharp]  view plain copy
  1. namespace WpfApplication1 {  
  2.     public class Window1ViewModel {  
  3.   
  4.         public ICommand ButtonCommand {  
  5.             get {  
  6.                 return new DelegateCommand<string>((str) => {  
  7.                     MessageBox.Show("Button's parameter:"+str);  
  8.                 });  
  9.             }  
  10.         }  
  11.   
  12.     }  
  13. }  


并且,特殊情况下,我们可以将控件对象作为参数传递给ViewModel,注意{Binding RelativeSource={x:Static RelativeSource.Self}}是绑定自己(Button)的意思。

[html]  view plain copy
  1. <Window x:Class="WpfApplication1.Window1"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         xmlns:vm="clr-namespace:WpfApplication1"  
  5.         Title="Window1" Height="193" Width="190">  
  6.     <Window.DataContext>  
  7.         <vm:Window1ViewModel />  
  8.     </Window.DataContext>  
  9.     <Grid>  
  10.         <Button Content="Button" Height="33" HorizontalAlignment="Left" Margin="34,20,0,0" Name="button1" VerticalAlignment="Top" Width="109"  
  11.                 Command="{Binding ButtonCommand}"  
  12.                 CommandParameter="20"/>  
  13.         <Button Content="Button" Height="33" HorizontalAlignment="Left" Margin="34,85,0,0" Name="button2" VerticalAlignment="Top" Width="109"  
  14.                 Command="{Binding ButtonCommand2}"  
  15.                 CommandParameter="{Binding RelativeSource={x:Static RelativeSource.Self}}"/>  
  16.     </Grid>  
  17. </Window>  

再看ViewModel

[csharp]  view plain copy
  1. namespace WpfApplication1 {  
  2.     public class Window1ViewModel {  
  3.   
  4.         public ICommand ButtonCommand {  
  5.             get {  
  6.                 return new DelegateCommand<string>((str) => {  
  7.                     MessageBox.Show("Button's parameter:"+str);  
  8.                 });  
  9.             }  
  10.         }  
  11.   
  12.         public ICommand ButtonCommand2 {  
  13.             get {  
  14.                 return new DelegateCommand<Button>((button) => {  
  15.                     button.Content = "Clicked";  
  16.                     MessageBox.Show("Button");  
  17.                 });  
  18.             }  
  19.         }  
  20.     }  
  21. }  


这样就可以在委托中获取Button对象了。但是MVVM本身比建议ViewModel操作View。

下面介绍一种更强大的命令绑定,可以用于各种事件的ViewModel触发。


(三)任意事件的Command

WPF中不是所有的控件都有Command属性的,如果窗体我需要在ViewModel中处理Loaded事件命令,或者其他事件的命令时,很难都过绑定Command完成,必须要注册依赖属性或事件等,太麻烦了。我喜欢简约、有效的方式,现在我和大家一起分享一下。

场景,我需要处理Button的Click和MouseMove事件,但又避免用后置代码,尽量要在ViewModel中获取。单独一个Click可以通过Button的Command来完成,在前两篇文章中我已介绍过,现在就来处理MouseMove事件,这是需要一个System.Windows.Interactivity.dll,该dll是安装Blend后才有的,在C:\Program Files\Microsoft SDKs\Expression\Blend\.NETFramework\v4.0\Libraries目录中,然后我们仍需要Prism.dll。

xaml:

[html]  view plain copy
  1. <Window x:Class="WpfApplication1.Window2"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"  
  5.         xmlns:vm="clr-namespace:WpfApplication1"  
  6.         Title="Window2" Height="124" Width="214">  
  7.     <Window.DataContext>  
  8.         <vm:Window2ViewModel />  
  9.     </Window.DataContext>  
  10.     <Grid>  
  11.         <Button Name="btn" Content="Button" Height="33" HorizontalAlignment="Left" Margin="40,24,0,0" VerticalAlignment="Top" Width="109">  
  12.             <i:Interaction.Triggers>  
  13.                 <i:EventTrigger EventName="Click">  
  14.                     <i:InvokeCommandAction Command="{Binding Command1}" CommandParameter="10" />  
  15.                 </i:EventTrigger>  
  16.                 <i:EventTrigger EventName="MouseMove">  
  17.                     <i:InvokeCommandAction Command="{Binding Command2}" CommandParameter="{Binding ElementName=btn}" />  
  18.                 </i:EventTrigger>  
  19.             </i:Interaction.Triggers>  
  20.         </Button>  
  21.     </Grid>  
  22. </Window>  


注意;xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"就是导入Blend的dll,然后在控件内部用<i:Interaction.Triggers/>即可,其它应该一看就知道,我通过事件触发器,来引发ViewModel中两个Command,第二个Command的参数是Button对象,通过ElementName=btn来指定。

ViewModel:

[csharp]  view plain copy
  1. namespace WpfApplication1 {  
  2.     public class Window2ViewModel {  
  3.   
  4.         public ICommand Command1 {  
  5.             get {  
  6.                 return new DelegateCommand<string>((str) => {  
  7.                     MessageBox.Show("Command1 with parameter:"+str);  
  8.                 });  
  9.             }  
  10.         }  
  11.   
  12.         public ICommand Command2 {  
  13.             get {  
  14.                 return new DelegateCommand<Button>((button) => {  
  15.                     Point p = Mouse.GetPosition(button);  
  16.                     button.Content = string.Format("{0},{1}", p.X, p.Y);  
  17.                 });  
  18.             }  
  19.         }  
  20.     }  
  21. }  


这部分内容和上一章内容基本相同。

好了,测试一下,实现起来是不是非常简洁!(我以前是做Java的,所以代码简洁明了是一贯作风,生活也是如此!)

 

代码下载:http://qing2005.download.csdn.net/


转自:http://blog.csdn.net/qing2005/article/details/6601475


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 WPF MVVM 架构中,可以使用 InvokeCommandAction 来触发 ViewModel 中的命令,并且可以传递一个参数。如果需要传递多个参数,可以使用以下方法: 1. 使用命令参数对象 义一个类,包含需要传递的多个参数,例如: ``` public class CommandParameter { public string Parameter1 { get; set; } public int Parameter2 { get; set; } } ``` 在 XAML 中,使用该类作为 InvokeCommandAction 的 CommandParameter 属性的值: ``` <i:InvokeCommandAction Command="{Binding MyCommand}" CommandParameter="{Binding CommandParameter}" /> ``` 在 ViewModel 中,命令的 Execute 方法可以接收该类的实例作为参数: ``` public RelayCommand<CommandParameter> MyCommand { get; set; } public void MyCommandExecute(CommandParameter parameter) { // 使用参数 } ``` 2. 使用触发事件的参数 在 XAML 中,可以使用 EventTrigger 触发一个事件,并且可以使用 EventTrigger 的 EventArgsConverter 属性将事件参数转换为需要的类型。例如: ``` <i:Interaction.Triggers> <i:EventTrigger EventName="MyEvent"> <i:InvokeCommandAction Command="{Binding MyCommand}"> <i:InvokeCommandAction.CommandParameter> <MultiBinding Converter="{StaticResource MyConverter}"> <Binding Path="Parameter1" /> <Binding Path="Parameter2" /> </MultiBinding> </i:InvokeCommandAction.CommandParameter> </i:InvokeCommandAction> </i:EventTrigger> </i:Interaction.Triggers> ``` 在这里,使用 MultiBinding 将多个值传递给一个转换器。转换器将这些值转换为需要的类型,并且将它们封装到一个对象中,然后作为命令的参数传递给 ViewModel。 在 ViewModel 中,命令的 Execute 方法可以接收该对象作为参数: ``` public RelayCommand<MyParameter> MyCommand { get; set; } public void MyCommandExecute(MyParameter parameter) { // 使用参数 } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值