Click here to Skip to main content
14,690,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
I do not find why it shows me in the first line of listviewArticleManagement.Model.Article? and how can I fix this problem?

What I have tried:

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<Article> 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  
{  
    /// <summary>  
    /// This class contains properties that a View can data bind to.  
    /// <para>  
    /// See http://www.galasoft.ch/mvvm  
    /// </para>  
    /// </summary>  
    public class MainViewModel : ViewModelBase, IMainViewModel  
    {  
        /// <summary>  
        /// Initializes a new instance of the MainViewModel class.  
        /// </summary>  
   
        private readonly ObservableCollection<Article> article;  
        public MainViewModel()  
        {  
   
            article = new ObservableCollection<Article>();  
            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<Article> Articles  
        {  
            get { return this.article; }  
        }  
   
        public ICommand ChargerArticleCommand  
        {  
            get;  
            private set;  
        }  
    }  
}


ViewModelLocator.cs

/* 
  In App.xaml: 
  <Application.Resources> 
      <vm:ViewModelLocator xmlns:vm="clr-namespace:GestionArticle.ViewModel" 
                                   x:Key="Locator" /> 
  </Application.Resources> 
  
  In the View: 
  DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}" 
*/  
   
using GalaSoft.MvvmLight;  
using GalaSoft.MvvmLight.Ioc;  
using Microsoft.Practices.ServiceLocation;  
   
namespace GestionArticle.ViewModel  
{  
    /// <summary>  
    /// This class contains static references to all the view models in the  
    /// application and provides an entry point for the bindings.  
    /// <para>  
    /// See http://www.galasoft.ch/mvvm  
    /// </para>  
    /// </summary>  
    public class ViewModelLocator  
    {  
        static ViewModelLocator()  
        {  
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);  
   
            SimpleIoc.Default.Register<IMainViewModel, MainViewModel>();  
        }  
        public static IMainViewModel MainVM  
        {  
            get { return ServiceLocator.Current.GetInstance<IMainViewModel>(); }  
        }  
   
   
        /// <summary>  
        /// Gets the Main property.  
        /// </summary>  
        [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<MainViewModel>(); 
            } 
        }*/  
        public static void CleanMain()  
        {  
            SimpleIoc.Default.Unregister<IMainViewModel>();  
            SimpleIoc.Default.Register<IMainViewModel, MainViewModel>();  
        }  
        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>  
        <!--Global View Model Locator-->  
        <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}}">  
   
    <!-- <Window.Resources>  
        <ResourceDictionary>  
            <ResourceDictionary.MergedDictionaries>  
                <ResourceDictionary Source="Skins/MainSkin.xaml" />  
            </ResourceDictionary.MergedDictionaries>  
        </ResourceDictionary>  
    </Window.Resources>-->  
   
    <Grid x:Name="LayoutRoot">  
   
        <TextBlock FontSize="36"  
                   FontWeight="Bold"  
                   Foreground="#FFF3ACF3"  
                   Text="{Binding WelcomeTitle}"  
                   VerticalAlignment="Center"  
                   HorizontalAlignment="Center"  
                   TextWrapping="Wrap" Margin="317.5,138,76.5,194" Width="211" Background="#FF7FE6FD" />  
        <ListView HorizontalAlignment="Left" Height="225" Margin="59,31,0,0" VerticalAlignment="Top" Width="185" ItemsSource="{Binding Articles, Mode=OneWay}">  
            <ListView.View>  
                <GridView>  
                    <GridViewColumn/>  
                </GridView>  
            </ListView.View>  
        </ListView>  
        <Button Content="Ajouter" HorizontalAlignment="Left" Height="40" Margin="308,292,0,0" VerticalAlignment="Top" Width="162" Command="{Binding ChargerArticleCommand, Mode=OneWay}"/>  
   
    </Grid>  
</Window> 


when I run I can not find the article object insert by default:
image
Posted
Updated 31-Jan-18 14:15pm

1 solution

It looks like the Article model is added to the ListView however you have not set up your column correctly. Check out this article: [^]
   
Comments
abboudi_ammar 31-Jan-18 19:46pm
   
but in the MVVM design it's different. i must use the ObservableCollection and bind that with listview
Graeme_Grant 31-Jan-18 20:00pm
   
Sorry if I was not clear ... Yes, the ListView will only see changes in the Data Binding if you use a collection that implements the INotifyCollectionChanged & INotifyPropertyChanged interfaces.

I was referring to how to bind Model properties to ListView Columns.
abboudi_ammar 1-Feb-18 10:15am
   
"the ListView will only see changes in the Data Binding if you use a collection that implements the INotifyCollectionChanged & INotifyPropertyChanged interfaces." is the ObservableCollection enough to do that?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900