Create an application using MVVM and an application using MVVM

Source: Internet
Author: User

Create an application using MVVM and an application using MVVM

MVVM mode:
Use prism Microsoft. Practices. Prism. dll
Introduction to the WPF Interaction framework to add reference to the Interactions library. Add the following two DLL files:
Microsoft. Expression. Interactions. dll and System. Windows. Interactivity. dll
Must be referenced first
Xmlns: I = "http://schemas.microsoft.com/expression/2010/interactivity"
Xmlns: ig = "http://schemas.infragistics.com/xaml"

Call method:

<i:Interaction.Triggers>        <i:EventTrigger>            <i:InvokeCommandAction Command="{Binding LoadCommand}" />        </i:EventTrigger>    </i:Interaction.Triggers>
<! -- ComboBox Binding drop-down list --> <ComboBox x: Name = "cbYear" ItemsSource = "{Binding Path = YearList, Mode = TwoWay, updateSourceTrigger = PropertyChanged} "SelectedItem =" {Binding Path = SelectedYear} "> <I: Interaction. triggers> <I: EventTrigger EventName = "SelectionChanged" SourceObject = "{Binding ElementName = cbYear}"> <I: invokeCommandAction Command = "{Binding YearChangedCommand}" CommandName = "YearChangedCommand"/> </I: EventTrigger> </I: Interaction. triggers> </ComboBox>

Bind a button with Command such as Command = "{Binding LoginCommand }"

On the Design page: <Button Content = "login" Name = "btn_Login" Command = "{Binding LoginCommand}"> </Button>

In cs, you can also write:

this.btn_Login.Click += new RoutedEventHandler(btn_Login_Click);MainWindowViewModel viewModel = new MainWindowViewModel();void btn_Login_Click(object sender, RoutedEventArgs e){viewModel.LoginCommand.Execute();}

Next, we need to write the MainWindowViewModel corresponding to the view. If you need to bind the interface value, such as the textbox, the value can be dynamically displayed.
This requires Prism, and the class must be integrated with icationicationobject.

Public class MainWindowViewModel: icationicationobject {private string _ userName = string. empty; public string UserName {get {return _ userName;} set {_ userName = value; RaisePropertyChanged ("UserName") ;}// define Command public DelegateCommand loadcommnad{ get; set;} public MainWindowViewModel () {this. loginCommand = new DelegateCommand (Login); // Command call the Login method} // response Command private void Login () {if (string. isNullOrEmpty (UserName) {UserName = "UserName is blank"; // the front-end text box is displayed }}}

 

Related Article

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.