"2016-10-24" "persist in Learning" "Day11" "WPF" "MVVM"

Source: Internet
Author: User

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> &LT;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"

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.