在WPF中的ListView中添加项目 [英] Add items in ListView in WPF

查看:119
本文介绍了在WPF中的ListView中添加项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位大家好..有一个简单的问题。如何在没有创建特殊类的情况下在ListView中添加项目???



示例:



特殊类
公共类MyItem
{
public int Id {get;组; }
public string Name {get;组; }
}

XAML
< ListView x:名称 = listView >
< < span class =code-leadattribute> ListView.View >
< GridView >
< GridViewColumn 标题 = Id DisplayMemberBinding = {Binding Id} / >
< GridViewColumn 标题 = 名称 DisplayMemberBinding = {Binding Name} / >
< / GridView >
< / ListView.View >
< / ListView >

代码
public Window1()
{
this.InitializeComponent();
this.listView.Items.Add(new MyItem {Id = 1,Name =Davidwroxy});
}

解决方案

您需要先构建某种数据对象来保存20列数据, 1个对象包含1行。



那么



您将每行/对象添加到列表,集合或其他一些列表中'实现了IEnumerable接口。



您可以通过将List绑定到ListView来填充ListView。



当您获取数据时从源代码填充一个对象并将其放入列表中。



您应该有一个方法只需要输入并将其转换为您设计的对象

Hello everybody..have a little simple question. How I can add item in ListView without creation special class???

Example:

Special class
public class MyItem
{
    public int Id { get; set; }
    public string Name { get; set; }
}

XAML
<ListView x:Name="listView">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Id" DisplayMemberBinding="{Binding Id}"/>
            <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
        </GridView>
    </ListView.View>
</ListView>

Code
public Window1()
{
    this.InitializeComponent();
    this.listView.Items.Add(new MyItem { Id = 1, Name = "Davidwroxy" });
}

解决方案

You need to start by building some kind of data object to hold the '20' columns of data, 1 object holds 1 row.

THEN

You Add each row/object to a List, Collection, or some other 'list' that has the IEnumerable interface implemented.

You can fill the ListView by binding your List to the ListView.

When you get your data from the the source you fill an object and put it in the list.

You should have a methods that just takes your input and converts it to an object of your design.


这篇关于在WPF中的ListView中添加项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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