Http://www.cnblogs.com/zhili/p/MVVMDemo.html
first, the introduction
Some of the core aspects of WPF are described earlier, including WPF layouts, dependency properties, routed events, bindings, commands, resource styles, and templates. However, there is also a good programming framework in WPF, WVVM, where MVC is developed on the web side, and MVVM in WPF client development, where the VM is the equivalent of C (Control) in MVC. On the web side, Microsoft has developed an MVC framework such as ASP.net mvc, and in the WPF world, Microsoft has also developed MVVM frameworks such as Prism. Prism Project address is: Http://compositewpf.codeplex.com/SourceControl/latest. Everyone interested can download the source code research. This article all source code downloads: Fristmvvmproject.zip Two, MVVM mode is what.
When it comes to MVVM mode, the first question of nature is the meaning of MVVM. MVVM is an abbreviated form of model-view-viewmodel that is commonly used in WPF or Silverlight development. The relationship between the three is shown in the following illustration :
Here we introduce the three parts separately.
Models (model)
model--can be understood as a class with fields, attributes.
Views (view)
view--can be understood as the UI we see.
View Model
View model plays a connecting role between view and model, and separates the view and model layers. View model is not just model packaging, it also contains program logic, and model extensions, for example, if there is a public attribute in model that does not need to be displayed on the UI, then we can no longer define it in view model.
In MVVM mode, the running process of the WPF program is shown in the following illustration:
In MVVM, the VM's status can be said to be important. The use of MVVM mode has the following features: The view of the CS file includes very few code, its core logic are placed in the view model class, so that the program logic and view coupling reduced. ViewModel class as the DataContext of the view. Under MVVM, all events and actions are treated as commands, such as button clicks, which are not triggered by clicking Events, but are bound to a click Command, which is then executed by the command to perform the corresponding logic. use MVVM mode to implement WPF programs
The basics of MVVM are described earlier, with an example of how to apply the MVVM pattern in a WPF program. When you implement the WPF program before, we may put all the background logic in the view of the background file, such implementation of the benefits of a more intuitive, convenient, for some small applications to do so of course no problem, but for complex applications like this, may cause the background code appears very bloated, To the best becomes difficult to maintain. At this point the solution is the separation of duties, so that the logic of the background to separate the other classes, MVVM in fact, I understand is to achieve this goal. Below we follow the MVVM component to implement this MVVM program.
The first step: nature is the data part, that is, the model layer implementation. Here you define a person class that contains 2 basic properties.
public class person
{public
string Name {get; set;}
public int Age {get; set;}
}
For testing purposes, create a static method below to obtain test data.
public class Persondatahelper
{public
static observablecollection<person> getpersons ()
{
observablecollection<person> samplepersons = new observablecollection<person> ();
Samplepersons.add (new person () {Name = ' john ', age =});
Samplepersons.add (new person () {Name = "Harry", age=});
Samplepersons.add (new person () {Name = ' dick ', age =});
Samplepersons.add (new person () {Name = ' learninghard ', age =});
Return samplepersons
}
}
Step Two: Implement the ViewModel layer, and realize the logic between the data and the interface. In the View model class, the properties and commands are included, because in MVVM, events are handled as commands, where commands can only be bound to controls that have command properties. Now that you want to include the command, you need to implement a command, where the custom command needs to implement the ICommand interface. Here we define a querycommand. The specific implementation code looks like this:
public class Querycommand:icommand {#region Fields private Action _execute;
Private func<bool> _canexecute; #endregion public Querycommand (Action execute): This (execute, NULL) {} PU
Blic Querycommand (Action Execute, func<bool> canexecute) {if (execute = null)
throw new ArgumentNullException ("execute");
_execute = Execute;
_canexecute = CanExecute;
} #region ICommand member public event EventHandler canexecutechanged {add {if (_canexecute!= null) {commandmanager.requerysuggested = VA
Lue
} remove {if (_canexecute!= null) {
commandmanager.requerysuggested = value;
}
}
} public bool CanExecute (object parameter) {return _canexecute = = null? True: _canexecute ();
public void Execute (object parameter) {_execute (); } #endregion}
The next step is to define our ViewModel class, where the implementation code looks like this:
1 public class Personlistviewmodel:inotifypropertychanged 2 {3 #region Fields 4 private string
_searchtext;
5 Private observablecollection<person> _resultlist; 6 #endregion 7 8 #region Properties 9 public observablecollection<person> Personlis t {get; private set;} 11 12//Query key public string SearchText {Retu RN _searchtext; set {_searchtext = value raisepropertychanged ("Sea
Rchtext "); 20} 21} 22 23//Query Results public observablecollection<person> resultlist 25 {num get {return _resultlist;} _resultlist = VA
Lue
raisePropertyChanged ("Resultlist"); The public ICommand Querycommand 35 {36 get {return