WPF adopts the MVVM mode (binding: frontend only, command: trigger binding command), wpfmvvm
MVVM binding
View-viewModel-model, model introduction omitted, is to create a class, add a field encapsulation attribute. Note: The control can only be bound to attributes and cannot be bound to fields;
Next is the code
(View ):
1 <Window x: Class = "WpfBing. MainWindow" 2 xmlns =" http://schemas.microsoft.com/winfx/2006/xaml /Presentation "3 xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "4 xmlns: vm =" clr-namespace: WpfBing "5 xmlns: I =" clr-namespace: System. windows. interactivity; assembly = System. windows. interactivity "6 Title =" MainWindow "Height =" 350 "Width =" 525 "> 7 <Grid> 8 <Grid. dataContext> 9 <vm: ViewModel/> 10 </Grid. dataContext> 11 <TextBox Text = "{Binding Name, UpdateSourceTrigger = PropertyChanged}" Width = "150" Height = "30"> 12 <I: Interaction. triggers> 13 <I: EventTrigger EventName = "TextChanged"> 14 <I: InvokeCommandAction Command = "{Binding NameChanged}"/> 15 </I: EventTrigger> 16 </I: interaction. triggers> 17 </TextBox> 18 <Button Content = "test" Command = "{Binding UpdateData}" Width = "150" Height = "40" HorizontalAlignment = "Right"> 19 </Button> 20 </Grid> 21 </Window>
Note:
Xmlns: vm = "clr-namespace: WpfBing" Add a reference to the namespace, mainly to enable the front-end page to find the namespace of the viewmodel;
Xmlns: I = "clr-namespace: System. windows. interactivity; assembly = System. windows. interactivity is used to add reference to the wpf naming control, which is mainly used to provide the trigger binding command in the namespace.
Download link for this class: System. Windows. Interactivity
<Grid. dataContext> <vm: ViewModel/> </Grid. dataContext> data source binding: bind the data source of the space to the ViewModel object in the vm space. Note: The Key to frontend-only binding
<I: Interaction. Triggers>
<I: EventTrigger EventName = "TextChanged"> <I: InvokeCommandAction Command = "{Binding NameChanged}"/> </I: EventTrigger> </I: Interaction. Triggers>
Use a trigger to bind the control event command. This Code requires reference of System. Windows. Interactivity. dll.
(BaseClass ):
1 using System; 2 using System. collections. generic; 3 using System. diagnostics; 4 using System. linq; 5 using System. text; 6 using System. threading. tasks; 7 using System. windows. input; 8 9 namespace WpfBing10 {11 public class RelayCommand: ICommand12 {13 # region field 14 readonly Func <Boolean> _ canExecute; 15 readonly Action _ execute; 16 # endregion17 18 # region constructor 19 public RelayCommand (Action execute) 2 0: this (execute, null) 21 {22} 23 24 public RelayCommand (Action execute, Func <Boolean> canExecute) 25 {26 if (execute = null) 27 throw new ArgumentNullException ("execute"); 28 _ execute = execute; 29 _ canExecute = canExecute; 30} 31 # endregion32 33 # region ICommand member 34 public event EventHandler CanExecuteChanged35 {36 add37 {38 39 if (_ canExecute! = Null) 40 CommandManager. RequerySuggested + = value; 41} 42 remove43 {44 45 if (_ canExecute! = Null) 46 CommandManager. RequerySuggested-= value; 47} 48} 49 50 [DebuggerStepThrough] 51 public Boolean CanExecute (Object parameter) 52 {53 return _ canExecute = null? True: _ canExecute (); 54} 55 56 public void Execute (Object parameter) 57 {58 _ execute (); 59} 60 # endregion61} 62}
Note: This section of code mainly implements the ICommand to implement this command interface, and calls the corresponding method in ViewModel through a delegate call;
ICommand mainly has two methods: Excute and CanExcute. One is the implementation method of the call, and the other is to determine whether to execute the call method;
Note: The function can be used to control buttons or other items, and whether the control status is available.
(Viewmodel ):
Using System; using System. collections. generic; using System. componentModel; using System. linq; using System. text; using System. threading. tasks; using System. windows. input; namespace WpfBing {public class ViewModel: INotifyPropertyChanged {public event PropertyChangedEventHandler PropertyChanged; public void Policy (string name) {if (PropertyChanged! = Null) {PropertyChanged (this, new PropertyChangedEventArgs (name) ;}} private string name = "Test Data"; public string Name {get {return name ;} set {name = value; Policy ("Name") ;}} void UpdateArtistNameExecute () {this. name = "";} bool CanUpdateArtistNameExecute () {return true;} public ICommand UpdateData {get {return new RelayCommand (UpdateArtistNameExecute, CanUpdateArtistNameExecute );}} public ICommand NameChanged {get {return new RelayCommand (NameChang) ;}} private void NameChang () {string na = Name ;}}}
Note: In viewmodel, some event streams are controlled. Data streams can be controlled to refresh the Foreground Data, control commands, and access the business layer;