繁体   English   中英

如何动态地将array []绑定到DataGridTextColumn wpf

[英]How to Binding the array[] to the DataGridTextColumn dynamiclly wpf

当我有一个Staff类时,我想将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,但最终只能创建带有标题的列,并且没有要显示的数据,但是TB [i]有数据,但是不能像图片一样显示: 在此处输入图片说明

当我将代码更改为

c.Binding = new Binding("TB[" + i + "]");

它表明 在此处输入图片说明

这是您需要的...

c.Binding = new Binding("TB[" + i + "]");

还定义了结核病...

public float[] TB { get; set; }  = new float[100];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM