our

Latest Blogs

Splinter - WPF MVVM Framework

iFour Team -July 07, 2021

Listening is fun too.

Straighten your back and cherish with coffee - PLAY !

  •  
  •  
  •  
Splinter - WPF MVVM Framework

What is MVVM Framework in WPF?

MVVM (Model-View-ViewModel) is a software architecture design pattern that will allow you to utilize the Model-View-ViewModel design pattern in the WPF application. The MVVM is a design pattern that will divide your application into three layers (Model, View, ViewModel).

  • Model: Here we can define business logic and data. The model can represent the actual data or information (like name, contact, address, phone number, date of birth, etc.).
  • View: In, view we can set UI using the visual elements like labels, buttons, editors, etc., and bind it with View Model.
  • ViewModel: Here we can define the logic that connects the View and the Model. For managing a list of contacts view model is designed.
MVVM Framework

 

Here we depict significant features of MVVM Framework.

  • ViewModel: These classes are used for implementing validation, consuming service, commands, bindable properties, and interaction between View Models.
  • Services: We can define all the services in View and we can use their functionality in View Models.
  • Commands: We can bind the Commands in the View and define their action in View Model.
  • DXBinding: The Binding can extend with the DXCommand, DXEvent, and DXBinding markup extensions.
  • MIF: MIF stands for Module Injection Framework. This is a collection of classes. That can allow you to organize an MVVM application.
  • Behaviors: We can extend the functionality of visual control and comply with the MVVM pattern.

For developing a simple application, MVVM can provide some components.

  • ViewLocator: Enable you to locate views by their type name.
  • Messenger: Allow you to exchange the message between modules.
  • Converters: Allow you to perform conversions between different types and between the value of the same type.
  • Data Annotation Attributes Support: These attributes can be used to customize data classes. Data annotation attributes can define validation rules, set relationships between data classes, and specify how data is displayed from a data source.
  • Weak Event: Weak event can implement interaction between different application modules (like data services and view models) and objects.
  • LayoutTreeHelper: LayoutTreeHelper can provide an API. And this API is used for searching nodes and iterating through elements of logical and visual trees.

Building Block of MVVM Architecture

ICommand: ICommand is used to control the behavior such as enable, disable of control behavior on execution. ICommand interface can also provide the code contract for the commanding behavior of Runtime XAML.

INotifyPropertyChanged: This interface can define when the value of a property change. It can be raised PropertyChanged event when the value changes.

Binding to Property: Below components are used for data binding as it helps to present data in the view. Their four-component for data binding.

  • Binding target object
  • Target property
  • Binding source
  • Path of the value in the binding source to use

We can set data flow in four ways.

  • OneWay
  • TwoWay
  • OneWayToSource
  • OneTime

Let’s see a simple example of MVVM Framework. Here we can add the Student details.

Step: 1

First create a WPF application, after successful creation of the application create a three folder in the root application and the name should be View, Model, and ViewModel.

Step: 2

Add a new class in the Model folder. For add new class, Right-click of the Model folder -> Add -> Class -> Add class name and press OK.

After adding a class create their properties, here the Student is our class name let’s see below code.

In this example we can add Student details like FirstName and LastName for this here we can add two properties FirstName and LastName.

using System.ComponentModel.DataAnnotations;

namespace WPFMVVMFramework.Model
{
    public class StudentData
    {
        [Key]
        public int StudentNumber{ get; set; }
        public string StudentFirstOrLastName{ get; set; }
        public string StudentCast{ get; set; }
    }
}
Step: 3

After that create one more class for DbContext. DbContext is used to write and execute the queries, bind objects in memory to UI controls

using System.Data.Entity;

namespace WPFMVVMFramework.Model
{
    public class Contextdb : DbContext
    {
        public Contextdb() : base("Conn")
        {

        }
        public DbSet Studentinformation { get; set; }
    }
}
Step: 4

Now, create a ViewModel, right-click on the ViewModel folder and add a new class.

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using WPFMVVMFramework.Model;

namespace WPFMVVMFramework.ViewModel
{
    public class AddDataViewModel : ViewModelBase
    {
        public AddDataViewModel()
        {
            DataList = new ObservableCollection(contextdb.Studentinformation);
        }

        private StudentData _studentObj = new StudentData();

        public StudentData StudentObj
        {
            get 
            { 
                return _studentObj; 
            }
            set 
            { 
                _studentObj = value; 
                RaisePropertyChanged(); 
            }
        }

        public ICommand Userlist => new RelayCommand(adddata1);
        public Contextdb contextdb = new Contextdb();
        public ObservableCollection _dataList;
        public ObservableCollection DataList
        {
            get 
            { 
                return _dataList; 
            }
            set 
            { 
                _dataList = value; 
                RaisePropertyChanged(); 
            }
        }
        private void adddata1()
        {
            contextdb.Studentinformation.Add(_studentObj);
            contextdb.SaveChanges();
            MessageBox.Show("Insert Successfully.....");
            DataList = new ObservableCollection(contextdb.Studentinformation);


        }
    }
}

Here, is the code to add student data. In this ViewModel, we can add ObservableCollection for a list of student data, add their properties, and add MessageBox for showing the success message.

One Stop Solution for WPF Application Development ? Your Search ends here.

Step: 5

Now, we create a design for adding Student data.


 
 
 
 



















    

Here, we can bind the View with the ViewModel. In this view, we can use the grid for adding data and DataGrid for showing data.

Looking to hire WPF developers? Our seasoned experts are ready to support you. Reach out to us with your requirements.


Output:
Fill the data

Image: Fill the data

Success Message

Image: Success Message

Data Added Successfully

Image: Data Added Successfully

Conclusion

The prior advantage of using MVVM Framework is to provide a clear separation between business logic and presentation. Using MVVM Framework we can accelerate speed and performance enhancements.

Work with us

Well do everything we can to make our next best project!

Our Insights

Check out our most recent blogs

An in-depth guide on Angular Dependency Providers
An in-depth guide on Angular Dependency Providers

What is Angular? Angular is a frontend development framework used for building single-page client applications using HTML and Typescript. It is written in Typescript. What...

A simple guide on AOT Compilation in Angular
A simple guide on AOT Compilation in Angular

What is a Compiler? A compiler is nothing but a part of code that converts one programming language to another. If we talk about some simple programming languages like C, C++,...

Implement Attribute, Class, and Style binding in Angular
Implement Attribute, Class, and Style binding in Angular

What is binding in Angular? Binding in angular apps is the automatic synchronization of data within the model and view components. You can use data binding to define things...

Our Partners

Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo

Work With Us

Well do everything we can to make our next best project!