迭代 ObservableCollection<T> 值
Iterating over ObservableCollection<T> values
我正在尝试创建一个库,用于从可观察集合在 WPF 中创建报告,但我不知道如何循环 ObservableCollection
以将值写入 FlowDocument
table.我尝试将通用集合转换为 IEnumerable
,但我不断得到
Exception thrown: 'System.NullReferenceException'
目标是编写一个接受所有 ObservableCollection
类型的库,所以我写了这个 class(片段):
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
class :
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
以在 table 行中写入值。
目前此代码仅将类型写入单元格,例如My_Project.Model.Employee
。
有人可以告诉我如何正确执行此操作吗?
P.S.: 如你所见,我正在遵循 MVVM 方法。
假设这是 class 你有
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) });
}
我正在尝试创建一个库,用于从可观察集合在 WPF 中创建报告,但我不知道如何循环 ObservableCollection
以将值写入 FlowDocument
table.我尝试将通用集合转换为 IEnumerable
,但我不断得到
Exception thrown: 'System.NullReferenceException'
目标是编写一个接受所有 ObservableCollection
类型的库,所以我写了这个 class(片段):
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
class :
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
以在 table 行中写入值。
目前此代码仅将类型写入单元格,例如My_Project.Model.Employee
。
有人可以告诉我如何正确执行此操作吗?
P.S.: 如你所见,我正在遵循 MVVM 方法。
假设这是 class 你有
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) });
}