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 }}}