【问题标题】:How to Binding the array[] to the DataGridTextColumn dynamiclly wpf如何将数组 [] 动态绑定到 DataGridTextColumn wpf
【发布时间】:2017-03-12 00:40:51
【问题描述】:

当我有一个 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# wpf wpfdatagrid


【解决方案1】:

这就是你需要的……

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

也是结核病的定义...

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

【讨论】:

  • 我不知道如何在评论中显示我的屏幕截图
  • 列头变成了1-N,但是DataGridTextColumn没有绑定数据,没有数据显示
  • 我很抱歉。我编辑了答案以使解决方案完整。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-05
  • 2019-07-27
  • 1970-01-01
  • 1970-01-01
  • 2014-04-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多