WPF的ListView怎么动态的添加数据

WPF的ListView怎么动态的添加数据,第1张

listView.Items.Add(要增加的对象),代码如下:

var p = new Person { Name = "张三", Address = "上海市乳山路", Sex = SexEnum.男 }

listView.Items.Add(p)

//Help class

public enum SexEnum { 男, 女 }

public class Person

{

public string Name { getset}

public string Address { getset}

public SexEnum Sex { getset}

}

提供两种方法:

1.如果是手动添加数据,只能通过循环实现

2.如果是根据类实现的数据绑定,只要在类中添加一个bool变量,默认值为true,绑定到checkbox就ok。这种情况下可以通过让类继承INotifyPropertyChanged 类的列表继承ObservableCollection实现。

附上一段代码,看不懂百度HI我。

using System

using System.Collections.Generic

using System.Linq

using System.Text

using System.ComponentModel

using System.Collections.ObjectModel

namespace AddrBook

{

public class People : INotifyPropertyChanged

{

public event PropertyChangedEventHandler PropertyChanged

private void NotifyPropertyChanged(String propName)

{

if (PropertyChanged != null)

{

PropertyChanged(this, new PropertyChangedEventArgs(propName))

}

}

private string name = string.Empty

public string Name

{

get { return name}

set

{

if (name == value)

{

return

}

else

{

name = value

NotifyPropertyChanged("Name")

}

}

}

private string phonenum = string.Empty

public string PhoneNum

{

get { return phonenum}

set

{

if (phonenum == value)

{

return

}

else

{

phonenum = value

NotifyPropertyChanged("PhoneNum")

}

}

}

private bool? ischecked = false

public bool? IsChecked

{

get { return ischecked}

set

{

if (ischecked == value)

{

return

}

else

{

ischecked = value

NotifyPropertyChanged("IsChecked")

}

}

}

}

public class Person : ObservableCollection<People>{ }

public class Group : INotifyPropertyChanged

{

public event PropertyChangedEventHandler PropertyChanged

private void NotifyPropertyChanged(String propName)

{

if (PropertyChanged != null)

{

PropertyChanged(this, new PropertyChangedEventArgs(propName))

}

}

private string groupname

public string GroupName

{

get { return groupname}

set

{

if (groupname == value)

{

return

}

else

{

groupname = value

NotifyPropertyChanged("GroupName")

}

}

}

private Person groupmember = new Person()

public Person GroupMember

{

get { return groupmember}

set

{

if (groupmember == value)

{

return

}

else

{

groupmember = value

NotifyPropertyChanged("GroupMember")

}

}

}

private int membercount = 0

public int MemberCount

{

get { return membercount}

set

{

if (membercount == value)

{

return

}

else

{

membercount = value

NotifyPropertyChanged("MemberCount")

}

}

}

private bool? ischecked = false

public bool? IsChecked

{

get { return ischecked}

set

{

if (ischecked == value)

{

return

}

else

{

ischecked = value

NotifyPropertyChanged("IsChecked")

NotifyMember(ischecked)

}

}

}

private void NotifyMember(bool? ischeck)

{

if (groupmember.Count >0)

{

foreach (People p in groupmember)

{

p.IsChecked = ischeck

}

}

}

}

public class GroupList : ObservableCollection<Group>{ }

}

还是给你整个文件吧


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11834219.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存