Transfer from http://blog.csdn.net/qing2005/article/details/6601199
http://blog.csdn.net/qing2005/article/details/6601475
command binding is easily implemented in MVVM (ii) Pass command arguments
Property bar to set the. The statement should be commandparameter= "{Binding elementname= control name}"
If we need to pass parameters in command, the implementation is simple. Delegatecommand also has a delegatecommand<t> version that can pass a parameter of type T.
A button binding of 1.View, where CommandParameter defines a parameter of "20"
[HTML]View Plaincopy
- <window x:class="Wpfapplication1.window1"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:vm="Clr-namespace:wpfapplication1"
- title= "Window1" height="193" width="> "
- <window.datacontext>
- <vm:window1viewmodel />
- </window.datacontext>
- <Grid>
- <Button < span class= "attribute" >content= "button" height= " horizontalalignment= margin=" 34,20,0,0 " name= "button1" Verticalalignment= "Top" width= "109" &NBSP;&NBSP;
- command="{Binding Buttoncommand}"
- commandparameter="/> "
- </Grid>
- </Window>
2.ViewModel define commands, note delegate parameters
[CSharp]View Plaincopy
- Namespace WpfApplication1 {
- public class Window1viewmodel {
- Public ICommand Buttoncommand {
- get {
- return new delegatecommand<string> ((str) + = {
- MessageBox.Show ("button ' s parameter:" +STR);
- });
- }
- }
- }
- }
And, in special cases, we can pass the control object as a parameter to ViewModel, noting that {binding relativesource={x:static relativesource.self}} is the meaning of binding itself (Button).
[HTML]View Plaincopy
- <window x:class="Wpfapplication1.window1"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:vm="Clr-namespace:wpfapplication1"
- title= "Window1" height="193" width="> "
- <window.datacontext>
- <vm:window1viewmodel />
- </window.datacontext>
- <Grid>
- <button content="button" height="horizontalalignment=" "left " margin= "34,20,0,0" name="button1" verticalalignment="Top" width="109 "
- command="{Binding Buttoncommand}"
- commandparameter="/> "
- < Button content= "button" height= "all" "left" margin=" 34,85,0,0 " name=" Button2 " < span class= "attribute" >verticalalignment= "Top" width = "109" &NBSP;&NBSP;
- command="{Binding ButtonCommand2}"
- commandparameter="{Binding relativesource={x:static Relativesource.self}}"/>
- </Grid>
- </Window>
Look at ViewModel again.
[CSharp]View Plaincopy
- Namespace WpfApplication1 {
- public class Window1viewmodel {
- Public ICommand Buttoncommand {
- get {
- return new delegatecommand<string> ((str) + = {
- MessageBox.Show ("button ' s parameter:" +STR);
- });
- }
- }
- Public ICommand ButtonCommand2 {
- get {
- return new delegatecommand<button> (Button) = {
- button. Content = "Clicked";
- MessageBox.Show ("button");
- });
- }
- }
- }
- }
This allows you to get the button object in the delegate. But MVVM itself is better than suggesting ViewModel operation view.
Notes on how WPF obtains view Control objects in ViewModel in MVVM mode