MVVM中轻松实现Command绑定(二)传递Command参数
2013-02-05 21:38
423 查看
我们如果需要在Command中传递参数,实现也很简单。DelegateCommand还有一个DelegateCommand<T>版本,可以传递一个T类型的参数。
1.View的Button绑定,其中CommandParameter定义了一个“Baby”的参数
<Window x:Class="MVVMCommand.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MVVMCommand"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:MainWindowViewModel/>
</Window.DataContext>
<Grid>
<Button Content="Button" Command="{Binding ButtonCommand}"
CommandParameter="Baby"/>
</Grid>
</Window>
2.ViewModel定义命令,注意委托参数
namespace MVVMCommand{
public class MainWindowViewModel{
public ICommand ButtonCommand{
get{
return new DelegateCommand<string>(new Action<string>((str) =>{
MessageBox.Show("Button's parameter: " + str);
}));
}
}
}
}
并且,特殊情况下,我们可以将控件对象作为参数传递给ViewModel,注意{Binding RelativeSource={x:Static RelativeSource.Self}}是绑定自己(Button)的意思。
<Window x:Class="MVVMCommand.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MVVMCommand"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:MainWindowViewModel/>
</Window.DataContext>
<Grid>
<Button Content="Button" Command="{Binding ButtonCommand}"
CommandParameter="{Binding RelativeSource={x:Static RelativeSource.Self}}"/>
</Grid>
</Window>
再看ViewModel
namespace MVVMCommand{
public class MainWindowViewModel{
public ICommand ButtonCommand{
get{
return new DelegateCommand<Button>(new Action<Button>((btn) =>{
btn.Content = "Clicked";
}));
}
}
}
}
1.View的Button绑定,其中CommandParameter定义了一个“Baby”的参数
<Window x:Class="MVVMCommand.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MVVMCommand"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:MainWindowViewModel/>
</Window.DataContext>
<Grid>
<Button Content="Button" Command="{Binding ButtonCommand}"
CommandParameter="Baby"/>
</Grid>
</Window>
2.ViewModel定义命令,注意委托参数
namespace MVVMCommand{
public class MainWindowViewModel{
public ICommand ButtonCommand{
get{
return new DelegateCommand<string>(new Action<string>((str) =>{
MessageBox.Show("Button's parameter: " + str);
}));
}
}
}
}
并且,特殊情况下,我们可以将控件对象作为参数传递给ViewModel,注意{Binding RelativeSource={x:Static RelativeSource.Self}}是绑定自己(Button)的意思。
<Window x:Class="MVVMCommand.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MVVMCommand"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:MainWindowViewModel/>
</Window.DataContext>
<Grid>
<Button Content="Button" Command="{Binding ButtonCommand}"
CommandParameter="{Binding RelativeSource={x:Static RelativeSource.Self}}"/>
</Grid>
</Window>
再看ViewModel
namespace MVVMCommand{
public class MainWindowViewModel{
public ICommand ButtonCommand{
get{
return new DelegateCommand<Button>(new Action<Button>((btn) =>{
btn.Content = "Clicked";
}));
}
}
}
}
相关文章推荐
- MVVM中轻松实现Command绑定(二)传递Command参数
- MVVM中轻松实现Command绑定(四)获取事件参数EventArgs
- MVVM中轻松实现Command绑定(五)获取事件参数EventArgs(2)
- 管窥MVVMLight Command参数绑定和事件传递
- MVVM中轻松实现Command绑定(三)任意事件的Command
- MVVM中轻松实现Command绑定任意事件的Command
- MVVM中轻松实现Command绑定(三)任意事件的Command
- MVVM中轻松实现Command绑定(三)任意事件的Command
- MVVM中轻松实现Command绑定(一)简单绑定
- MVVM中轻松实现Command绑定(一)简单绑定
- Windows Phone 7 MVVM模式数据绑定和传递参数
- JQuery 绑定事件时传递参数的实现方法
- 浅析Java中通过ThreadLocal实现线程绑定来传递参数
- Windwos Phone MVVM Light的 EventToCommand 命令绑定实现方式
- Windows Phone 7 MVVM模式数据绑定和传递参数
- MVVMLight Toolkit在Windows Phone中的使用扩展之一:在ViewModel中实现导航,并传递参数
- Windows Phone 7 MVVM模式数据绑定和传递参数
- Windows Phone 7 MVVM模式数据绑定和传递参数
- Windows Phone 7 MVVM模式数据绑定和传递参数
- MVVM模式的View与ViewModel的三大通讯方式:Binding Data(实现数据的传递)、Command(实现操作的调用)和Attached Behavior【转】