C# 如何在wpf listvew c中添加子项#
我想在wpf中插入列表视图子项 我正在使用此代码在windows窗体listview中插入子项C# 如何在wpf listvew c中添加子项#,c#,wpf,listview,C#,Wpf,Listview,我想在wpf中插入列表视图子项 我正在使用此代码在windows窗体listview中插入子项 con.Open(); DataTable dt = new DataTable(); DataSet ds = new DataSet(); ds.Tables.Add(dt); OleDbDataAdapter da = new OleDbDataAdapter("select * from test where (SecSym='" + secsym + "')", con); da = ne
con.Open();
DataTable dt = new DataTable();
DataSet ds = new DataSet();
ds.Tables.Add(dt);
OleDbDataAdapter da = new OleDbDataAdapter("select * from test where (SecSym='" + secsym + "')", con);
da = new OleDbDataAdapter("select * from test where (SecSym='" + secsym + "')order by Date desc", con);
da.Fill(dt);
int iRecords = 0;
foreach (DataRow myrow in dt.Rows)
{
ListViewItem lItem = new ListViewItem();
lItem.UseItemStyleForSubItems = false;
DateTime Date = DateTime.ParseExact(myrow[3].ToString(), "yyyyMMdd", CultureInfo.CurrentCulture);
string date = Date.ToString("ddd, dd-MMM-yyyy");
lItem = listviewTargets.Items.Insert(iRecords, date);
lItem.UseItemStyleForSubItems = false;
// listviewTargets.Items.Add(myrow[2].ToString());
lItem.SubItems.Add(myrow[1].ToString());
lItem.SubItems.Add(myrow[15].ToString());
lItem.SubItems.Add(myrow[5].ToString(), Color.White, Color.Green, lItem.Font);
lItem.SubItems.Add(myrow[7].ToString());
lItem.SubItems.Add(myrow[8].ToString());
lItem.SubItems.Add(myrow[9].ToString());
lItem.SubItems.Add(myrow[10].ToString());
iRecords++;
lItem = listviewTargets.Items.Insert(iRecords, "");
lItem.UseItemStyleForSubItems = false;
lItem.SubItems.Add("");
lItem.SubItems.Add("");
lItem.SubItems.Add(myrow[6].ToString(), Color.White, Color.Red, lItem.Font);
lItem.SubItems.Add(myrow[11].ToString());
lItem.SubItems.Add(myrow[12].ToString());
lItem.SubItems.Add(myrow[13].ToString());
lItem.SubItems.Add(myrow[14].ToString());
iRecords++;
}
con.Close();
但我不知道如何在wpf listview中插入子项
如果有人能告诉我,我将不胜感激
提前感谢。WPF列表视图有点不同,没有“子项” 您可以实现以下目标: 在xaml中定义Listview(将数据绑定到集合):
此示例的代码隐藏:
public partial class MainWindow : Window
{
public class Person
{
public string Name {get;set;}
public DateTime DOB {get;set;}
}
public IList<Person> People { get; set; }
public MainWindow()
{
People = new List<Person>()
{
new Person() {Name = "Martin", DOB = DateTime.Now.AddYears(-20)},
new Person() {Name = "Lilo", DOB = DateTime.Now.AddYears(-25)}
};
InitializeComponent();
this.DataContext = this;
}
}
公共部分类主窗口:窗口
{
公共阶层人士
{
公共字符串名称{get;set;}
公共日期时间DOB{get;set;}
}
公共IList人员{get;set;}
公共主窗口()
{
人员=新列表()
{
newperson(){Name=“Martin”,DOB=DateTime.Now.AddYears(-20)},
newperson(){Name=“Lilo”,DOB=DateTime.Now.AddYears(-25)}
};
初始化组件();
this.DataContext=this;
}
}