Learn about the MVVM of WPF today
They say, aps.net ===>MVC
Wpf===>mvvm
Using WPF instead of MVVM, it's better to use WinForm ...
Haha, off-topic.
Defined:
MVVM:
The combination of WPF data binding and presentation model is a good practice, allowing developers to separate view and logic, but this data binding technique is very simple and practical, and is unique to WPF. So we also call it Model-view-viewmodel (MVVM). This model is similar to the classic MVP (Model-view-presenter) model, except that you need a model that is tailored to the View, which is viewmodel. ViewModel contains all UI-specific interfaces and properties, and is bound by a ViewModel view, and can be loosely coupled, so you need to write the code in the ViewModel direct update view. The data-binding system also supports the validation of input that provides a standardized way of transmitting validation errors to views.
Model, data models,
View, views, interface
ViewModel, the view model, used to connect the view, and the middle of the model.
Public class Model { publicstringgetset;} Public int Get Set ; } }
Mdoel
<window x:class="Wpfapplication1.mainwindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"XMLNS:MC="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="Clr-namespace:wpfapplication1"mc:ignorable="D"Title="MainWindow"height=" -"Width="525"> <Grid> <Grid.ColumnDefinitions> <columndefinition></columndefinition > <ColumnDefinition></ColumnDefinition> </Grid.ColumnDefinitions> <LISTBO X x:name="ListBox"Itemssource="{Binding Models}"Displaymemberpath="Name"selecteditem="{Binding Selectedmodel,mode=twoway}"> </ListBox> <stackpanel grid.column="1"Verticalalignment="Center"> <textblock text="name"></TextBlock> <textbox text="{Binding Selectedmodel.name}"></TextBox> <textblock text="Age"></TextBlock> <textbox text="{Binding Selectedmodel.age}"></TextBox> </StackPanel> </Grid></Window>View
Public Partial class Mainwindow:window { public MainWindow () { InitializeComponent (); This New ViewModel (); } }
View behind Code
Public Abstract classviewmodelbase:inotifypropertychanged {#regionProperty Changed Event Handler Public voidSetpropertychanged (stringPropertyName) { if(PropertyChanged! =NULL) PropertyChanged ( This,NewPropertyChangedEventArgs (PropertyName)); } Public EventPropertyChangedEventHandler propertychanged; #endregionProperty Changed Event Handler } Public classViewmodel:viewmodelbase { PublicObservablecollection<model> Models {Get;Set; } PrivateModel Selectedmodel; PublicModel Selectedmodel {Get{returnSelectedmodel;} Set{Selectedmodel=value; Setpropertychanged ("Selectedmodel"); } } PublicViewModel () {Models=GetData (); } PublicObservablecollection<model>GetData () {returnGetdatafromdb (); } PrivateObservablecollection<model>Getdatafromdb () {ObservableCollection<Model> result =NewObservablecollection<model>(); for(inti =0; I < -; i++) {result. ADD (NewModel () {Name="Name"+I, age=I,}); } returnresult; } }ViewModel
Add:
In fact, you can not use the interface Behindcode assignment DataContext, directly in the interface reference.
<Window.DataContext> <local:ViewModel/> </Window.DataContext>
2016-10-24, "persist in Learning" "Day11" "WPF" "MVVM"