abdelwaheb ammar

abdelwaheb ammar

  • 1.3k
  • 393
  • 118.6k

WPF application with MVVM

Jan 31 2018 5:40 PM
Good evening everyone, I am looking to develop a simple application to understand MVVM architecture with WPF. I just want to insert a text entered in a text field entered on a listview when pressing the add button.
 
I create two folders that are Template and ViewModel and here is the source code insert for each file knowing that I use the MVVMLightToolkit extension.
 
for the template folder:
 
Article.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.    
  6. namespace GestionArticle.Model  
  7. {  
  8.   public class Article  
  9.     {  
  10.         private string _nom;  
  11.    
  12.         public string Nom  
  13.         {  
  14.             get { return _nom; }  
  15.             set { _nom = value; }  
  16.         }  
  17.     }  
  18. }  
and for the ViewModel folder:
IMainViewModel.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Collections.ObjectModel;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Windows.Input;  
  7. using GestionArticle.Model;  
  8.    
  9. namespace GestionArticle.ViewModel  
  10. {  
  11.   public  interface IMainViewModel  
  12.     {  
  13.         string Titre { getset; }  
  14.         ObservableCollection Articles { get; }  
  15.         ICommand ChargerArticleCommand { get; }  
  16.     }  
  17. }  
MainViewModel.cs
  1. using GalaSoft.MvvmLight;  
  2. using System.Collections.ObjectModel;  
  3. using GestionArticle.Model;  
  4. using GalaSoft.MvvmLight.Command;  
  5. using System.Windows.Input;  
  6. namespace GestionArticle.ViewModel  
  7. {  
  8.     ///   
  9.     /// This class contains properties that a View can data bind to.  
  10.     ///    
  11.     /// See http://www.galasoft.ch/mvvm  
  12.     ///   
  13.     ///   
  14.     public class MainViewModel : ViewModelBase, IMainViewModel  
  15.     {  
  16.         ///   
  17.         /// Initializes a new instance of the MainViewModel class.  
  18.         ///   
  19.    
  20.         private readonly ObservableCollection article;  
  21.         public MainViewModel()  
  22.         {  
  23.    
  24.             article = new ObservableCollection();  
  25.             article.Add(new Article { Nom = "article 1" });  
  26.             ChargerArticleCommand = new RelayCommand(ChargerArticles);  
  27.         }  
  28.    
  29.         private void ChargerArticles()  
  30.         {  
  31.             this.article.Add(new Article { Nom = "Article 2" });  
  32.         }  
  33.    
  34.         private string _titre;  
  35.         public string Titre  
  36.         {  
  37.             get { return _titre; }  
  38.             set  
  39.             {  
  40.                 _titre = value;  
  41.                 RaisePropertyChanged("Titre");  
  42.             }  
  43.         }  
  44.    
  45.         public ObservableCollection Articles  
  46.         {  
  47.             get { return this.article; }  
  48.         }  
  49.    
  50.         public ICommand ChargerArticleCommand  
  51.         {  
  52.             get;  
  53.             private set;  
  54.         }  
  55.     }  
  56. }  
ViewModelLocator.cs
  1. /* 
  2.   In App.xaml: 
  3.    
  4.        
  5.                                    x:Key="Locator" /> 
  6.    
  7.   
  8.   In the View: 
  9.   DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}" 
  10. */  
  11.    
  12. using GalaSoft.MvvmLight;  
  13. using GalaSoft.MvvmLight.Ioc;  
  14. using Microsoft.Practices.ServiceLocation;  
  15.    
  16. namespace GestionArticle.ViewModel  
  17. {  
  18.     ///   
  19.     /// This class contains static references to all the view models in the  
  20.     /// application and provides an entry point for the bindings.  
  21.     ///    
  22.     /// See http://www.galasoft.ch/mvvm  
  23.     ///   
  24.     ///   
  25.     public class ViewModelLocator  
  26.     {  
  27.         static ViewModelLocator()  
  28.         {  
  29.             ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);  
  30.    
  31.             SimpleIoc.Default.Register();  
  32.         }  
  33.         public static IMainViewModel MainVM  
  34.         {  
  35.             get { return ServiceLocator.Current.GetInstance(); }  
  36.         }  
  37.    
  38.    
  39.         ///   
  40.         /// Gets the Main property.  
  41.         ///   
  42.         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",  
  43.             "CA1822:MarkMembersAsStatic",  
  44.             Justification = "This non-static member is needed for data binding purposes.")]  
  45.        /* public MainViewModel Main 
  46.         { 
  47.             get 
  48.             { 
  49.                 return ServiceLocator.Current.GetInstance(); 
  50.             } 
  51.         }*/  
  52.         public static void CleanMain()  
  53.         {  
  54.             SimpleIoc.Default.Unregister();  
  55.             SimpleIoc.Default.Register();  
  56.         }  
  57.         public static void Cleanup()  
  58.         {  
  59.             CleanMain();  
  60.         }  
  61.     }  
  62. }  
App.xaml
  1. <Application x:Class="GestionArticle.App"  
  2.              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  5.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  6.              xmlns:viewModel="clr-namespace:GestionArticle.ViewModel"  
  7.              StartupUri="MainWindow.xaml"  
  8.              mc:Ignorable="d">  
  9.    
  10.     <Application.Resources>  
  11.           
  12.         <viewModel:ViewModelLocator x:Key="Locator"  
  13.                              d:IsDataSource="True" />  
  14.     Application.Resources>  
  15.    
  16. Application>  
MainWindow.xaml
  1. <Window x:Class="GestionArticle.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  6.         xmlns:ignore="http://www.ignore.com"  
  7.         mc:Ignorable="d ignore"  
  8.         Height="410"  
  9.         Width="613"  
  10.         Title="MVVM Light Application"  
  11.    
  12.         DataContext="{Binding MainVM, Source={StaticResource Locator}}">  
  13.    
  14.     

Attachment: GestionArticle.zip

Answers (2)