简体   繁体   English

C# WPF 如何以编程方式填充我的 DataGrid

[英]C# WPF How to fill my DataGrid programmatically

I ve just started learning WPF, and trying to set the DataGrid with some dataset.我刚刚开始学习 WPF,并尝试使用一些数据集设置 DataGrid。 I have the appointments from Outlook, and its week numbers.我有来自 Outlook 的约会及其周数。 in lst I have list of the appointments that have properties, one of them is "cwHours" - calendar week hours - float array, indexes of which corresponds to calendar week numbers.在 lst 我有具有属性的约会列表,其中之一是“cwHours” - 日历周小时 - 浮点数组,其索引对应于日历周数。 And the values inside are working hours.里面的值是工作时间。 So that's the problem screenshot As seen on the screenshot, my array of cwhours is displayed in the column as Single array, But it should be splitted in columns by its indexes in the right side of project name.所以这就是问题screenshot如屏幕截图所示,我的 cwhours 数组在列中显示为单个数组,但它应该按项目名称右侧的索引拆分为列。 How can I put the array into the DataGrid, not in one column but in different ones?如何将数组放入 DataGrid 中,而不是在一列中,而是在不同的列中? Thank you.谢谢你。

        List<ProjectModel> lst = reader.olProjectList;
        DataTable.ItemsSource = lst;

        foreach(var x in lst)
        {
            float[] arrayofcw = x.cwHours;

            for (int i = 0; i < arrayofcw.Length; i++)
            {
                var col = new DataGridTextColumn();

                col.Header = i;
                DataTable.Columns.Add(col);
            }





        }

You should set the Binding property of the column.您应该设置列的Binding属性。 You could set the Path of the Binding to an index in the array property, eg:您可以将BindingPath设置为数组属性中的索引,例如:

var col = new DataGridTextColumn();
col.Binding = new Binding("cwHours[" + i + "]");
col.Header = i;
DataTable.Columns.Add(col);

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

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