Use of the WPF mvvmlight relaycommand binding command

Source: Internet
Author: User

Relaycommand

The biggest feature of MVVM is the separation of view and ViewModel, separating the display of data from the business logic. Using the binding of WPF, we can not only
Bind data from ViewModel to view, and you can bind the behavior to view. For example, click on a button on the main screen and this button actually completes
The operation is the corresponding method in ViewModel. Here we use the Relaycommand in the MVVM framework. Here are a few common scenarios

Relaycommand with no parameters

Click the button to pop up the message box
Appview.xaml

<Grid>        <Button Width="100" Height="30" Command="{Binding ShowMsgCommand}"></Button></Grid>

AppViewModel.cs

        ///<summary>Show message Commands///</summary>Public Relaycommand Showmsgcommand {get; set;} public Appviewmodel () { //Initialize command showmsgcommand= new Relaycommand (showmsg);}        //<summary>///For specific implementation///</summary> private void ShowMsg () {MessageBox.Show ("helloworld!");}              
Relaycommand with parameters

Click the button to display the text we entered
Appview.xaml

<Grid><Textboxx:name= "txt" width=  "height=" > </textbox> < button width= "100" height= "$" command=  "{Binding Showtxtcommand}" commandparameter=  "{Binding elementname=txt,path=text}" margin=  "0,100,0,0" ></Button></GRID>         

AppViewModel.cs

    ///<summary>Show message Commands///</summary>Public relaycommand<String> Showtxtcommand {get; set;} public AppViewModel (//initialize command Showtxtcommand = new RelayCommand <string> (showmsg);} ///<summary> ///command specifically implemented ///</summary> private void showmsg (string TXT) {MessageBox.Show (TXT);}              
Relaycommand is executable

Note that this is a very useful feature that will cause the buttons on the interface to be disabled when the Relaycommand execution conditions are not met. The judgment of the condition
is performed automatically by the WPF program and is very high frequency, so it should be as simple as possible to determine whether executable code should be executed.

Appview.xaml

<Grid><Textboxx:name= "txt" width=  "height=" 30 " text= "{Binding txt,updatesourcetrigger=propertychanged}" >< Span class= "Hljs-tag" ></textbox> < button width= "100" height= "$" command=  "{Binding Showtxtcommand}" margin= " 0,100,0,0 "></button></GRID>             

AppViewModel.cs

       PrivateString _txt;///<summary>TXT bound to the interface///</summary>PublicString Txt {get {return _txt; }set {_txt =Value raisePropertyChanged (() = TXT); } }///<summary>Show message Commands///</summary>Public Relaycommand Showtxtcommand {GetSet }PublicAppviewmodel () {Initialize command Showtxtcommand =New Relaycommand (ShowMsg, Canshowmsgexecute); }///<summary>command-specific implementation///</summary>private void SHOWMSG (///<summary> ///Show message command can execute // /</summary> ///<returns></returns> private bool canshowmsgexecute (return! String. IsNullOrEmpty (TXT); } 

Note: If you are using the. Net4.5, the button on the interface may not be disabled, which is a bug in MVVM, but the author has fixed it and resolved
Program look here.

The use of Relaycommand is so simple.

Http://www.cnblogs.com/HelloMyWorld/p/4750062.html

Use of the WPF mvvmlight relaycommand binding command

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.