[英]Comma-separated column value in DataGrid in WPF
I am using DataGrid
in WPF.我在 WPF 中使用
DataGrid
。 I am binding list of Teacher
class to the DataGrid
.我将
Teacher
类的列表绑定到DataGrid
。
class Teacher
{
public string Name{get;set;}
public int Age{get;set;}
public List<Subject> Subjects {get;set;}
}
class Subject
{
public string Name{get;set;}
public int Weight {get;set;}
}
I have setup the DataGrid
for columns "Teacher Name", "Age", "Subjects".我已经为“教师姓名”、“年龄”、“主题”列设置了
DataGrid
。 I wan the third column "Subjects" to have comma-separated list of Subject
class' Name
property belonging to Subjects
property of Teacher
class.我希望第三列“主题”具有逗号分隔的
Subject
类Name
属性列表,该属性属于Teacher
类的Subjects
属性。
How to do it ?怎么做 ?
Add a property to the class that returns the subject comma seperated using string.Join.向类中添加一个属性,该属性返回使用 string.Join 分隔的主题逗号。 Bind the property to the grid.
将属性绑定到网格。
Check below for using string.Join:检查下面是否使用 string.Join:
List<string> val = new List<string>();
val.Add("A");
val.Add("B");
val.Add("C");
string res = string.Join(", ", from item in val select item);
Hope it helps.希望能帮助到你。
If the Teacher obj is t1.如果教师 obj 是 t1。
Try to assign values to grid like:尝试将值分配给网格,例如:
t1.Name
t1.Age
string.Join(", ", from s in t1.Subjects select s.Name)
This way no need to add property for subject.这样就不需要为主题添加属性。 Try to do like this.
尝试这样做。
Hope it helps.希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.