TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
abdelwaheb ammar
1.3k
457
124k
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
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
GestionArticle.Model
{
public
class
Article
{
private
string
_nom;
public
string
Nom
{
get
{
return
_nom; }
set
{ _nom = value; }
}
}
}
and for the ViewModel folder:
IMainViewModel.cs
using
System;
using
System.Collections.Generic;
using
System.Collections.ObjectModel;
using
System.Linq;
using
System.Text;
using
System.Windows.Input;
using
GestionArticle.Model;
namespace
GestionArticle.ViewModel
{
public
interface
IMainViewModel
{
string
Titre {
get
;
set
; }
ObservableCollection Articles {
get
; }
ICommand ChargerArticleCommand {
get
; }
}
}
MainViewModel.cs
using
GalaSoft.MvvmLight;
using
System.Collections.ObjectModel;
using
GestionArticle.Model;
using
GalaSoft.MvvmLight.Command;
using
System.Windows.Input;
namespace
GestionArticle.ViewModel
{
///
/// This class contains properties that a View can data bind to.
///
/// See http://www.galasoft.ch/mvvm
///
///
public
class
MainViewModel : ViewModelBase, IMainViewModel
{
///
/// Initializes a new instance of the MainViewModel class.
///
private
readonly
ObservableCollection article;
public
MainViewModel()
{
article =
new
ObservableCollection();
article.Add(
new
Article { Nom =
"article 1"
});
ChargerArticleCommand =
new
RelayCommand(ChargerArticles);
}
private
void
ChargerArticles()
{
this
.article.Add(
new
Article { Nom =
"Article 2"
});
}
private
string
_titre;
public
string
Titre
{
get
{
return
_titre; }
set
{
_titre = value;
RaisePropertyChanged(
"Titre"
);
}
}
public
ObservableCollection Articles
{
get
{
return
this
.article; }
}
public
ICommand ChargerArticleCommand
{
get
;
private
set
;
}
}
}
ViewModelLocator.cs
/*
In App.xaml:
x:Key="Locator" />
In the View:
DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
*/
using
GalaSoft.MvvmLight;
using
GalaSoft.MvvmLight.Ioc;
using
Microsoft.Practices.ServiceLocation;
namespace
GestionArticle.ViewModel
{
///
/// This class contains static references to all the view models in the
/// application and provides an entry point for the bindings.
///
/// See http://www.galasoft.ch/mvvm
///
///
public
class
ViewModelLocator
{
static
ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
SimpleIoc.Default.Register
();
}
public
static
IMainViewModel MainVM
{
get
{
return
ServiceLocator.Current.GetInstance
(); }
}
///
/// Gets the Main property.
///
[System.Diagnostics.CodeAnalysis.SuppressMessage(
"Microsoft.Performance"
,
"CA1822:MarkMembersAsStatic"
,
Justification =
"This non-static member is needed for data binding purposes."
)]
/* public MainViewModel Main
{
get
{
return ServiceLocator.Current.GetInstance
();
}
}*/
public
static
void
CleanMain()
{
SimpleIoc.Default.Unregister
();
SimpleIoc.Default.Register
();
}
public
static
void
Cleanup()
{
CleanMain();
}
}
}
App.xaml
<
Application
x:Class
=
"GestionArticle.App"
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:viewModel
=
"clr-namespace:GestionArticle.ViewModel"
StartupUri
=
"MainWindow.xaml"
mc:Ignorable
=
"d"
>
<
Application.Resources
>
<
viewModel:ViewModelLocator
x:Key
=
"Locator"
d:IsDataSource
=
"True"
/>
Application.Resources
>
Application
>
MainWindow.xaml
<
Window
x:Class
=
"GestionArticle.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:ignore
=
"http://www.ignore.com"
mc:Ignorable
=
"d ignore"
Height
=
"410"
Width
=
"613"
Title
=
"MVVM Light Application"
DataContext
=
"{Binding MainVM, Source={StaticResource Locator}}"
>
Attachment:
GestionArticle.zip
Reply
Answers (
2
)
Caliburn Micro - Event Aggregator
i want to display image on image control when i select path