如何动态地将数组 [] 绑定到 DataGridTextColumn wpf
How to Binding the array[] to the DataGridTextColumn dynamiclly wpf
当我有员工 class 时,我想将 TB[] 绑定到 DataGrid 列
Staff.cs
public class Staff
{
public string Name { get; set; }
public float? Test_Score { get; set; }
public float? Net_Income_Tax { get; set; }
public float temp { get; set; }
public float[] TB = new float[100];
public float TB_Sum { get; set; }
}
在页面中,我的代码如下:
public ObservableCollection<Staff> Staff_Show = new ObservableCollection<Staff>();
public int StaffShowSize;
public TaxBefore_Sum(ObservableCollection<Staff> StaffAccept)
{
InitializeComponent();
Staff_Show = StaffAccept;
StaffShowSize = Staff_Show.Count;
DataGrid1.ItemsSource = this.Staff_Show;
//this is to dynamiclly to create new DataGridTextColumn,but
//final ,only create the columns' header is"0",and no data to show,however,the TB[i] have data,but can't show like the picture
for (int i = 0; i < StaffShowSize; i++)
{
DataGridTextColumn c = new DataGridTextColumn();
c.Header = Convert.ToString(i + 1);
c.Binding = new Binding("TB[i]");
DataGrid1.Columns.Add(c);
}
}
"for()"句是动态创建新的DataGridTextColumn,但是
最后,只创建了header的columns',没有数据显示,但是TB[i]有数据,但是不能显示如图:
当我将代码更改为
c.Binding = new Binding("TB[" + i + "]");
表示
这是您需要的...
c.Binding = new Binding("TB[" + i + "]");
还有 TB 的定义...
public float[] TB { get; set; } = new float[100];
当我有员工 class 时,我想将 TB[] 绑定到 DataGrid 列 Staff.cs
public class Staff
{
public string Name { get; set; }
public float? Test_Score { get; set; }
public float? Net_Income_Tax { get; set; }
public float temp { get; set; }
public float[] TB = new float[100];
public float TB_Sum { get; set; }
}
在页面中,我的代码如下:
public ObservableCollection<Staff> Staff_Show = new ObservableCollection<Staff>();
public int StaffShowSize;
public TaxBefore_Sum(ObservableCollection<Staff> StaffAccept)
{
InitializeComponent();
Staff_Show = StaffAccept;
StaffShowSize = Staff_Show.Count;
DataGrid1.ItemsSource = this.Staff_Show;
//this is to dynamiclly to create new DataGridTextColumn,but
//final ,only create the columns' header is"0",and no data to show,however,the TB[i] have data,but can't show like the picture
for (int i = 0; i < StaffShowSize; i++)
{
DataGridTextColumn c = new DataGridTextColumn();
c.Header = Convert.ToString(i + 1);
c.Binding = new Binding("TB[i]");
DataGrid1.Columns.Add(c);
}
}
"for()"句是动态创建新的DataGridTextColumn,但是
最后,只创建了header的columns',没有数据显示,但是TB[i]有数据,但是不能显示如图:
当我将代码更改为
c.Binding = new Binding("TB[" + i + "]");
表示
这是您需要的...
c.Binding = new Binding("TB[" + i + "]");
还有 TB 的定义...
public float[] TB { get; set; } = new float[100];