【问题标题】:How to add subitems in wpf listvew c#如何在wpf listview c#中添加子项
【发布时间】:2011-12-27 07:33:08
【问题描述】:

我想在 wpf 中插入列表视图子项。

我正在使用此代码在 Windows 表单列表视图中插入子项,

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 中插入子项。

如果有人能告诉我,将不胜感激。

提前致谢。

【问题讨论】:

    标签: c# wpf listview


    【解决方案1】:

    WPF 列表视图有点不同,没有“子项” 您可以通过以下方式实现您的目标:

    在 xaml 中定义 Listview(将数据绑定到集合):

    <ListView ItemsSource="{Binding People}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
                <GridViewColumn Header="Date of Birth" DisplayMemberBinding="{Binding DOB}"/>
            </GridView>
        </ListView.View>
    </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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      相关资源
      最近更新 更多