简体   繁体   English

在WPF中将数据添加到动态DataGrid

[英]Adding data to a dynamic DataGrid in WPF

Alright, I've been scratching my head over this one for the better part of the day, Google has been of little help, my fellow project mates have not been able to solve it either and we're less than a day away from our deadline. 好吧,我一天来大部分时间都在努力工作,Google对此几乎没有帮助,我的项目伙伴也都无法解决它,我们离我们只有不到一天的时间最后期限。 Help us Obi-One Stackoverflow, you're our only hope: 帮助我们Obi-One Stackoverflow,您是我们唯一的希望:

I have a DataGrid which consists of a variable number of columns of variable types (DataGridTextColumn, DataGridComboBoxColumn, DataGridCheckBoxColumn) 我有一个DataGrid,它由可变类型(DataGridTextColumn,DataGridComboBoxColumn,DataGridCheckBoxColumn)的可变数量的列组成

and I need to bind data to them. 我需要将数据绑定到它们。

Each of these columns will represent a database-query which is the reason for the grid having to be very flexible. 这些列中的每一列都代表一个数据库查询,这就是网格必须非常灵活的原因。 What I need help with is how to bind and add data to these columns in the code-behind part. 我需要帮助的是如何在代码隐藏部分中将数据绑定并添加到这些列。

EDIT It seems I forgot to make this clear: some columns will contain data of the same type (in our instance: "Grade"). 编辑似乎我忘了澄清这一点:有些列将包含相同类型的数据(在我们的实例中为“ Grade”)。 Depending on the subtype of this object we will need a different type for the column. 根据该对象的子类型,我们将需要为列使用其他类型。 As such, the Grid will have n columns, some of which have to be bound to data of the same type. 这样,网格将具有n列,其中某些列必须绑定到相同类型的数据。

END EDIT 结束编辑

I cannot design a container-class which has one Property for each column since they change dynamically, From what I've read I cannot bind the columns to different indices of an array, and the last approach-idea I had is to bind the different columns to a string representing the type of the data ("checkbox", "string", "combobox") and then simply add a container which has a property with that name to each individual column. 我不能设计一个容器类,因为每个列都会动态变化,所以每个列都具有一个属性。根据我的阅读,我无法将列绑定到数组的不同索引,而我最后一个方法思想是绑定不同的列列到代表数据类型的字符串(“复选框”,“字符串”,“组合框”)中,然后只需向每个单独的列添加一个具有该名称的属性的容器。 I couldn't find a way to do this since there doesn't seem to be an "Add" method to invoke on the grid's columns! 我找不到执行此操作的方法,因为似乎没有在网格的列上调用的“添加”方法! To summarize in wishful-thinking-code what I need is something looking like this: 用一厢情愿的代码总结一下,我需要的东西看起来像这样:

//Create an arbitrary number of columns
for(int i = 0; i < NR_CHECKBOXES; ++i) {
    DataGridCheckBoxColumn col = new DataGridCheckBoxColumn();
    col.Header = titles[i];
    //which are bound to a container with correct type of data
    col.Binding = new Binding("checkboxes[" + i + "]");
    grid.Columns.Add(col);
}

grid.Add(checkboxes); //and then populate the grid

or something like this: 或类似这样的东西:

//Create an arbitrary number of columns
for(int i = 0; i < NR_CHECKBOXES; ++i) {
    DataGridCheckBoxColumn col = new DataGridCheckBoxColumn();
    col.Header = titles[i];
    col.Binding = new Binding("Data");
    col.Add(checkboxes[i]); //Populate the column specifically
    grid.Columns.Add(col);
}

where checkboxes is a list of objects that has the property 'Data' (sorry, couldn't codify this inline by pressing tab then $, probably since I'm on a Swedish keyboard). 其中复选框是具有属性“数据”的对象的列表(对不起,无法通过按Tab键然后按$来编码该内联,可能是因为我在瑞典语键盘上)。 These loops would then be copied for each type of column and data I have (ComboBoxes and TextBoxes). 然后,将为我拥有的每种列和数据类型(ComboBoxes和TextBoxes)复制这些循环。

I hope this is enough to explain my problem, and that someone out there knows the proper way to achieve this in WPF. 我希望这足以解释我的问题,并且希望外面的人知道在WPF中实现此目标的正确方法。

Use a datatemplate. 使用数据模板。 Cast each query as a specific type and specify a datatemplate for that type. 将每个查询强制转换为特定类型,并为该类型指定数据模板。 You can add all the XAML properties you ever want for that type. 您可以为该类型添加所有想要的XAML属性。 WPF will pick the correct template based on the type. WPF将根据类型选择正确的模板。

DataTemplates 数据模板

Ok, I see your result set is very dynamic. 好的,我看到您的结果集非常动态。 Can you get what you want by creating a DataTemplate at runtime? 通过在运行时创建DataTemplate可以得到想要的东西吗?

Creating a DataTemplate at runtime 在运行时创建DataTemplate

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

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