Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good nigth people.

Im working in a wpf project(Standalone App) for my job and recently my boss told me that apply MVVM pattern, so I have so many questions because I dont understand it so much. Now I wanna know how to add a article to a listview with a Button, I saw so much tutorials but anyway explain about that.

Thank you so much.
Posted

The listview should be bound to a collection in the viewModel, the button should execute a command in the ViewModel.

The command execution should add an article to the collection.

Because the list view is bound to the collection, the new entry should be shown.
 
Share this answer
 
Comments
Member 10360777 10-Dec-13 11:15am    
OK, I understand you. Look my current code and tell me whats is wrong because I think that I have it.

<pre lang="c#">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Windows.Input;


namespace CatalogoProductos
{
//This is my ViewModel
public class ProductoViewModel
{
private IList<Producto> mPelicula;
public ProductoViewModel()
{
mPelicula = new List<Producto>
{
new Producto {Titulo="Pelicula1",Genero="Genero1",ActorPrincipal="Actor Principal1",Director="Director1",Productor="Productor1",BandaSonora="Banda Sonora1"},
new Producto {Titulo="Pelicula2",Genero="Genero2",ActorPrincipal="Actor Principal2",Director="Director2",Productor="Productor2",BandaSonora="Banda Sonora2"},
};
}

public IList<Producto> Peliculas
{
get {
return mPelicula;
}
set {
mPelicula = value;
}
}

public IList<Producto> mNuevaPelicula;
private ICommand mAgregar;
public ICommand Agregar
{
get {
if (mAgregar == null)
{
mAgregar = new AddPelicula();
}
return mAgregar;
}
set
{
mAgregar = value;

}
}

private class AddPelicula : ICommand
{

public bool CanExecute(object parametro)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parametro)
{
}
}
}
}
</pre>

<pre lang="xaml">

<!--AND THIS IS MY VIEW WITH THE BINDINGS. LOOK IT AND HELP ME PLEASE. THANK YOU-->
<grid>
<ListView Name="ListaPeliculas" Margin="0,190,0,0" ItemsSource="{Binding Peliculas}" >
<ListView.View>
<gridview x:name="grdPeliculas">
<gridviewcolumn header="Titulo" displaymemberbinding="{Binding Titulo}" width="100">
<gridviewcolumn header="Genero" displaymemberbinding="{Binding Genero}" width="100">
<gridviewcolumn header="Actor Principal" displaymemberbinding="{Binding ActorPrincipal}" width="100">
<gridviewcolumn header="Director" displaymemberbinding="{Binding Director}" width="100">
<gridviewcolumn header="Productor" displaymemberbinding="{Binding Productor}" width="100">
<gridviewcolumn header="Banda Sonora" displaymemberbinding="{Binding BandaSonora}" width="100">

</ListView.View>
</ListView>
<Button Content="Ingresar" HorizontalAlignment="Left" Margin="380,86,0,0" VerticalAlignment="Top" Width="115" Command="{Binding Path=Agregar}" />
<textbox x:name="txtTitulo" horizontalalignment="Left" height="23" margin="149,7,0,0" textwrapping="Wrap" verticalalignment="Top" width="195" text="{Binding ElementName=ListaPeliculas, Path=SelectedItem.Titulo}">
<textbox x:name="txtGenero" horizontalalignment="Left" height="23" margin="149,37,0,0" textwrapping="Wrap" verticalalignment="Top" width="195" text="{Binding ElementName=ListaPeliculas, Path=SelectedItem.Genero}">
<textbox x:name="txtActor" horizontalalignment="Left" height="23" margin="149,70,0,0" textwrapping="Wrap" verticalalignment="Top" width="195" text="{Binding ElementName=ListaPeliculas, Path=SelectedItem.ActorPrincipal}">
OK, I understand you. Look my current code and tell me whats is wrong because I think that I have it.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Windows.Input;


namespace CatalogoProductos
{
    //This is my ViewModel
    public class ProductoViewModel
    {
        private IList<producto> mPelicula;
        public ProductoViewModel()
        {
            mPelicula = new List<producto>
            {
                new Producto {Titulo="Pelicula1",Genero="Genero1",ActorPrincipal="Actor Principal1",Director="Director1",Productor="Productor1",BandaSonora="Banda Sonora1"},
                new Producto {Titulo="Pelicula2",Genero="Genero2",ActorPrincipal="Actor Principal2",Director="Director2",Productor="Productor2",BandaSonora="Banda Sonora2"},
            };
        }

        public IList<producto> Peliculas
        {
            get {
                return mPelicula;
            }
            set {
                mPelicula = value;
            }
        }

        public IList<producto> mNuevaPelicula;
        private ICommand mAgregar;
        public ICommand Agregar
        {
            get {
                    if (mAgregar == null)
                    {
                       mAgregar = new AddPelicula();
                    }
                    return mAgregar;
                }
            set
            {
                mAgregar = value;
              
            }
        }

        private class AddPelicula : ICommand
        {
            
            public bool CanExecute(object parametro)
            {
                return true;
            }
            public event EventHandler CanExecuteChanged;
            public void Execute(object parametro)
            {
            }
        }
    }
}
</producto></producto></producto></producto>


XAML
<!--AND THIS IS MY VIEW WITH THE BINDINGS. LOOK IT AND HELP ME PLEASE. THANK YOU-->
<grid>   
         <listview name="ListaPeliculas" margin="0,190,0,0" itemssource="{Binding Peliculas}">
            <listview.view>
                <gridview x:name="grdPeliculas" xmlns:x="#unknown">
                    <gridviewcolumn header="Titulo" displaymemberbinding="{Binding Titulo}" width="100" />
                    <gridviewcolumn header="Genero" displaymemberbinding="{Binding Genero}" width="100" />
                    <gridviewcolumn header="Actor Principal" displaymemberbinding="{Binding ActorPrincipal}" width="100" />
                    <gridviewcolumn header="Director" displaymemberbinding="{Binding Director}" width="100" />
                    <gridviewcolumn header="Productor" displaymemberbinding="{Binding Productor}" width="100" />
                    <gridviewcolumn header="Banda Sonora" displaymemberbinding="{Binding BandaSonora}" width="100" />
                </gridview>
            </listview.view>
        </listview>
        <Button Content="Ingresar" HorizontalAlignment="Left" Margin="380,86,0,0" VerticalAlignment="Top" Width="115" Command="{Binding Path=Agregar}" />
        <textbox x:name="txtTitulo" horizontalalignment="Left" height="23" margin="149,7,0,0" textwrapping="Wrap" verticalalignment="Top" width="195" text="{Binding ElementName=ListaPeliculas, Path=SelectedItem.Titulo}" xmlns:x="#unknown" />
        <textbox x:name="txtGenero" horizontalalignment="Left" height="23" margin="149,37,0,0" textwrapping="Wrap" verticalalignment="Top" width="195" text="{Binding ElementName=ListaPeliculas, Path=SelectedItem.Genero}" xmlns:x="#unknown" />
        <textbox x:name="txtActor" horizontalalignment="Left" height="23" margin="149,70,0,0" textwrapping="Wrap" verticalalignment="Top" width="195" text="{Binding ElementName=ListaPeliculas, Path=SelectedItem.ActorPrincipal}" xmlns:x="#unknown" />
        <textbox x:name="txtDirector" horizontalalignment="Left" height="23" margin="149,100,0,0" textwrapping="Wrap" verticalalignment="Top" width="195" text="{Binding ElementName=ListaPeliculas, Path=SelectedItem.Director}" xmlns:x="#unknown" />
        <textbox x:name="txtProductor" horizontalalignment="Left" height="23" margin="149,130,0,0" textwrapping="Wrap" verticalalignment="Top" width="195" text="{Binding ElementName=ListaPeliculas, Path=SelectedItem.Productor}" xmlns:x="#unknown" />
        <textbox x:name="txtBanda" horizontalalignment="Left" height="23" margin="149,162,0,0" textwrapping="Wrap" verticalalignment="Top" width="195" text="{Binding ElementName=ListaPeliculas, Path=SelectedItem.BandaSonora}" xmlns:x="#unknown" />
        <Label Content="Titulo" Name="lblTitutlo" HorizontalAlignment="Left" Margin="6,7,0,0" VerticalAlignment="Top" Width="108"/>
        <Label Content="Genero" Name="lblGenero" HorizontalAlignment="Left" Margin="6,34,0,0" VerticalAlignment="Top" Width="108"/>
        <Label Content="Actor Principal" Name="lblActor" HorizontalAlignment="Left" Margin="6,67,0,0" VerticalAlignment="Top" Width="108"/>
        <Label Content="Director" Name="lblDirector" HorizontalAlignment="Left" Margin="6,98,0,0" VerticalAlignment="Top" Width="108"/>
        <Label Content="Productor" Name="lblProductor" HorizontalAlignment="Left" Margin="6,127,0,0" VerticalAlignment="Top" Width="108"/>
        <Label Content="Banda Sonora" Name="lblBanda" HorizontalAlignment="Left" Margin="6,159,0,0" VerticalAlignment="Top" Width="108"/>
    </grid>
 
Share this answer
 

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