如何使用MVVM将文章添加到列表中。 WPF C# [英] how to add a article to a list with MVVM. WPF C#

查看:69
本文介绍了如何使用MVVM将文章添加到列表中。 WPF C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的nigth人。



我在一个wpf项目(独立应用程序)中工作,最近我的老板告诉我应用MVVM模式,所以我有这么多问题,因为我不太了解它。现在我想知道如何使用Button将文章添加到listview中,我看到了很多教程,但无论如何都要解释一下。



非常感谢你。

解决方案

listview应该绑定到viewModel中的一个集合,该按钮应该在ViewModel中执行一个命令。



命令执行应该在集合中添加一篇文章。



因为列表视图绑定到集合,所以应该显示新条目。

好的,我理解你。看看我现在的代码并告诉我什么是错的,因为我认为我有它。



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.ComponentModel;
使用 System.Windows.Input;


命名空间 CatalogoProductos
{
// 这是我的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 = < span class =code-string> Banda Sonora1},
new Producto {Titulo = Pelicula2,Genero = Genero2,ActorPrincipal = 演员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();
}
返回 mAgregar;
}
set
{
mAgregar = value ;

}
}

private class AddPelicula:ICommand
{

public bool CanExecute( object parametro)
{
return ;
}
public event EventHandler CanExecuteChanged;
public void 执行( object parametro)
{
}
}
}
}
< / < span class =code-leadattribute> producto
> < / producto > < / producto > < / producto >





 <! -    这是我的观点结合。看看并帮助我。谢谢   - >   
< grid >
< < span class =code-leadattribute> listview name = ListaPeliculas margin = 0,190,0,0 itemssource = {Binding Peliculas} >
< listview.view >
< gridview x:name = grdPeliculas xmlns:x = #未知 >
< gridviewcolumn header = Titulo displaymemberbinding = {Binding Titulo} width = 100 / >
< gridviewcolumn header = Genero displaymemberbinding = {Binding Genero} 宽度 = 100 / >
< gridviewcolumn header = Actor Principal displaymemberbinding = {Binding ActorPrincipal} width = 100 < span class =code-keyword> / >
< gridviewcolumn header = Director displaymemberbinding = {Binding Director} 宽度 = 100 / >
< gridviewcolumn header = 制作人 displaymemberbinding = {Binding Productor} width = 100 / >
< gridviewcolumn header = Banda Sonora displaymemberbinding = {Binding BandaSonora} width = 100 / >
< / gridview >
< / listview.view >
< / listview >
< < span class =code-leadattribute>按钮 内容 = Ingresar Horizo​​ntalAlignment = 保证金 = 380,86,0,0 VerticalAlignment = Top 宽度 = 115 命令 = {Binding Path = Agregar} / >
< textbox x:name = txtTitulo horizo​​ntalalignment = < span class =code-keyword>左 height = 23 margin = 149,7,0,0 textwrapping = 换行 verticalalignment = Top width = 195 text = {Binding ElementName = ListaPeliculas,Path = SelectedItem.Titulo} xmlns:x = #unknown / >
< textbox x:name = txtGenero horizo​​ntalalignment = height = 23 margin = 149,37,0,0 textwrapping = 换行 verticalalignment = Top width = < span class =code-keyword> 195 text = {Binding ElementName = ListaPeliculas,Path = SelectedItem.Genero} xmlns:x = #unknown / >
< textbox x:name = txtActor horizo​​ntalalignment = height = 23 margin = 149,70,0,0 textwrapping = 换行 verticalalignment = Top width = 195 text = {Binding ElementName = ListaPeliculas,Path = SelectedItem.ActorPrincipal} xmlns:x = #unknown / >
< 文本框 x:name = txtDirector horizo​​ntalalignment = < span class =code-attribute> 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 horizo​​ntalalignment = height = 23 margin = 149,130​​,0, 0 textwrapping = 换行 verticalalignment = Top width = 195 text = {Binding ElementName = ListaPeliculas,Path = SelectedItem.Productor} xmlns :x = #unknown / >
< textbox x:name = txtBanda horizo​​ntalalignment = height = 23 margin = 149,162,0,0 textwrapping = Wrap < span class =code-attribute> verticalalignment = Top widt h = 195 text = {Binding ElementName = ListaPeliculas,Path = SelectedItem.BandaSonora} xmlns:x = #unknown / >
< Label 内容 = Titulo 名称 = lblTitutlo Horizo​​ntalAlignment = Left 保证金 = < span class =code-keyword> 6,7,0,0 VerticalAlignment = Top 宽度 = 108 / >
< 标签 内容 = Genero 名称 = lblGenero Horizo​​ntalAlignment = 保证金 = 6,34,0,0 VerticalAlignment = Top 宽度 = 108 / >
< 标签 内容 = Actor Principal 名称 = lblActor Horizo​​ntalAlignment = 保证金 = 6,67,0,0 VerticalAlignment < span class =code-keyword> = Top 宽度 = 108 / >
< 标签 内容 = Director 名称 = lblDirector Horizo​​ntalAlignment = < span class =code-attribute> 保证金 = 6,98,0,0 VerticalAlignment = Top 宽度 = 108 / >
< 标签 内容 = 制作人 名称 = lblProductor Horizo​​ntalAlignment = 保证金 = 6,127,0,0 VerticalAlignment = Top 宽度 = 108 / >
< 标签 内容 = Banda Sonora 名称 = lblBanda Horizo​​ntalAlignment = 保证金 = 6,159,0,0 VerticalAlignment = Top 宽度 = 108 / >
< / grid >


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.

解决方案

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.


OK, I understand you. Look my current code and tell me whats is wrong because I think that I have it.

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>



<!--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>


这篇关于如何使用MVVM将文章添加到列表中。 WPF C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆