遍历ObservableCollection <T>值

我正在尝试创建一个库,用于在WPF中从可观察的集合中创建报表,但是我不知道如何循环ObservableCollection并将值写入FlowDocument表中。我尝试将通用集合转换为IEnumerable,但我不断获得

抛出异常:“ System.NullReferenceException”

目标是编写一个可以接受所有ObservableCollection类型的库,因此我已经编写了此类(代码段):

public class Report_Definition<T> : INotifyPropertyChanged
{
        public Report_Definition(FlowDocument doc,ObservableCollection<T> data)
        {
            Fill_data(data,doc);
        }

        private void Fill_data(ObservableCollection<T> data,FlowDocument doc)
        {
            //I only take 25 records from ObservableCollection - to create more Tables
            for (int i = 0; i < data.Count; i += 25)
            {
                var list = (from c in data select c).Skip(i).Take(25);

                 if (i < 25) //Test for only first Table
                {
                    for (int row = 0; row < list.Count(); row++)
                    {
                        TableRow my_cell = new TableRow();

                        foreach (var item in list)
                        {
                            foreach (var w in item as IEnumerable<T>) //ERROR HERE !!!
                            {
                                my_cell.Cells.Add(new TableCell(new Paragraph(new Run(w.ToString()))) { Padding = new Thickness(2),BorderBrush = brushes.Black,BorderThickness = new Thickness(0.7) });
                            }

                        }

                        TableRowGroup whole_row = new TableRowGroup();
                        whole_row.Rows.Add(my_cell);
                        My_table.RowGroups.Add(whole_row); //I get reference of this elsewhere...
                    }
                }
        }
}

这是我的Employee课:

public class Employee : INotifyPropertyChanged
{
        public string Name { get; set; }
        public string Surname { get; set; }
        public string Address { get; set; }
        public string Country { get; set; }

        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyName));
        }

        #endregion
}

因此,大写字母我使用ObservableCollection(Employee),并且我想遍历此集合中的每个Employee以在表行中写入值。

当前,此代码仅将类型写入单元格,例如My_Project.Model.Employee

有人可以告诉我如何正确执行此操作吗?

P.S .:如您所见,我正在采用MVVM方法。

benchimajun 回答:遍历ObservableCollection <T>值

假设这是您上的一堂课

public class Employee
{
    public string Name { get; set; }
    public string Surname { get; set; }
    public string Address { get; set; }
    public string Country { get; set; }
}

下面的代码将产生以下输出

    var props = typeof(Employee).GetProperties();

    var employee = new Employee { Name = "Athul",Surname = "Raj",Address = "Blah",Country = "India" };

    foreach (var prop in props)
    {
        Console.WriteLine($"{prop.Name} = {prop.GetValue(employee)}");
    }

    Console.ReadKey();

Name = Athul
Surname = Raj
Address = Blah
Country = India

您可以在方法中使用typeof(T).GetProperties()代替typeof(Employee).GetProperties()

,

要获取属性并为新单元格添加价值,可能需要执行以下操作:

var props = item.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var p in props) 
{
    my_cell.Cells.Add(new TableCell(new Paragraph(new Run(p.GetValue(this).ToString()))) { Padding = new Thickness(2),BorderBrush = Brushes.Black,BorderThickness = new Thickness(0.7) });
}
本文链接:https://www.f2er.com/2305133.html

大家都在问