Basic MVVM of WPF & SL

Source: Internet
Author: User

From http://www.cnblogs.com/itkm/archive/2012/03/30/2425732.html

When I was a beginner at MVVM, I always thought this was a mysterious thing. Then it was inexplicably recommended by the Prism framework on the Internet, and the project was almost out of control. In fact, we need to learn everything step by step. So I am talking again today. I will repeat this matter to hope it will be helpful to the later users. And exchange the development experiences of WPF & SL.

 

MVVM Definition

The explanation on the internet is short for Model-View-ViewModel. However, this is a bit difficult for beginners to understand.

Note: I have low IQ.

Let's explain it again in the vernacular: Model-is the Model (simply the database table or the element structure to be displayed)

View is the UI, the user interface, and The XAML file. The effect that is finally displayed to the user.

ViewModel-is the object that controls the View or corresponds to a Class in the code. This Class contains: a set of Model or Model, including the Command on The View)

Note: Generally, operations on the Model (in other words, operations on Data Objects) are placed in the Command. Interface interaction can also be performed through CommandParameter or other methods. If the operation is complex, consider using control in View.

 

Such a simple MVVM is OK.

The following is a simple example [not debugging is just to illustrate the problem, so the source code is not included ].

XAML: [View]

<Grid x: Name = "LayoutRoot">

<TextBlock Text = "{Binding StrName, Mode = OneWay}"> </TextBlock>

</Grid>

Model [cs]:

Public class XXClassModel: icationicationobject // [this reference can be directly used by using Microsoft. Practices. Prism. ViewModel ;]

{

Private string strName;

Public string StrName

{

Get {return strName ;}

Set {

StrName = value;

RaisePropertyChanged ("StrName"); // notification page, case sensitive

// More operations can be performed here, that is, the operations performed when the target attribute is assigned a value.

}

}

}

ViewModel [cs]:

Public class XXClassViewModel: icationicationobject

{

Public void XXClassViewModel

{

Item = new XXClassModel () {StrName = "OK "}

}

Private XXClassModel item;

Public XXClassModel Item

{

Get {return item ;}

Set {

StrName = value;

RaisePropertyChanged ("Item"); // notification page

// More operations can be performed here, that is, the operations performed when the target attribute is assigned a value.

}

}

}

Write it in the constructor of View. cs.

This. LayeroutRoot. DataContext = new XXClassViewModel ();

So far, all done!

 

 

When I was a beginner at MVVM, I always thought this was a mysterious thing. Then it was inexplicably recommended by the Prism framework on the Internet, and the project was almost out of control. In fact, we need to learn everything step by step. So I am talking again today. I will repeat this matter to hope it will be helpful to the later users. And exchange the development experiences of WPF & SL.

 

MVVM Definition

The explanation on the internet is short for Model-View-ViewModel. However, this is a bit difficult for beginners to understand.

Note: I have low IQ.

Let's explain it again in the vernacular: Model-is the Model (simply the database table or the element structure to be displayed)

View is the UI, the user interface, and The XAML file. The effect that is finally displayed to the user.

ViewModel-is the object that controls the View or corresponds to a Class in the code. This Class contains: a set of Model or Model, including the Command on The View)

Note: Generally, operations on the Model (in other words, operations on Data Objects) are placed in the Command. Interface interaction can also be performed through CommandParameter or other methods. If the operation is complex, consider using control in View.

 

Such a simple MVVM is OK.

The following is a simple example [not debugging is just to illustrate the problem, so the source code is not included ].

XAML: [View]

<Grid x: Name = "LayoutRoot">

<TextBlock Text = "{Binding StrName, Mode = OneWay}"> </TextBlock>

</Grid>

Model [cs]:

Public class XXClassModel: icationicationobject // [this reference can be directly used by using Microsoft. Practices. Prism. ViewModel ;]

{

Private string strName;

Public string StrName

{

Get {return strName ;}

Set {

StrName = value;

RaisePropertyChanged ("StrName"); // notification page, case sensitive

// More operations can be performed here, that is, the operations performed when the target attribute is assigned a value.

}

}

}

ViewModel [cs]:

Public class XXClassViewModel: icationicationobject

{

Public void XXClassViewModel

{

Item = new XXClassModel () {StrName = "OK "}

}

Private XXClassModel item;

Public XXClassModel Item

{

Get {return item ;}

Set {

StrName = value;

RaisePropertyChanged ("Item"); // notification page

// More operations can be performed here, that is, the operations performed when the target attribute is assigned a value.

}

}

}

Write it in the constructor of View. cs.

This. LayeroutRoot. DataContext = new XXClassViewModel ();

So far, all done!

 

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.