具有可变列数的 Infragistics XamDataGrid
我需要能够支持 XamDataGrid,它在设计时不会有一定数量的列。例如,应用程序将运行,从服务器获取一些数据并创建一些对象。根据服务器的响应,每次运行应用程序时可能会有不同数量的对象。
这是我的意思的一个例子。假设我调用某个服务并返回带有一些信息的 xml 响应。我将该响应反序列化为多个对象,每次调用时这些对象都可能不同。
假设每个对象都有两个属性:Label 和 Value。我希望网格显示带有标签的列,这些标签与“标签”的值与“值”中的值相匹配。因此,如果我有两个对象,obj1 和 obj2,看起来像这样:
obj1.Label = "Parts"
obj1.Value = "17"
obj2.Label = "Parts"
obj2.Value = "12"
我想要一个看起来像这样的网格,有两行:
Parts
17
12
如果我将数据源绑定到网格,网格自动仅使用对象的属性来创建列,因此我看到标签和值的列:
标签值
第 17 部分
第 12 部分
我假设我无法仅通过 xaml 实现我想要的目标。有人有我正在寻找的例子吗?是否只能由我在运行时以编程方式创建所有列?
I need to be able to support a XamDataGrid which at design time will not have a set number of columns. For example, the app will run, get some data from the server and create some objects. Depending on the response from the server, I may have a different number of objects each time I run the app.
Here is an example of what I mean. Lets say I make a call to some service and get back an xml response with some info. I deserialize that response into a number of objects, which can be different each time the call is made.
Lets say each object has two properties, Label and Value. I would like the grid to show columns with labels that match the value of Label with values from Value. So if I have a two objects, obj1 and obj2, that look like this:
obj1.Label = "Parts"
obj1.Value = "17"
obj2.Label = "Parts"
obj2.Value = "12"
I would like a grid that looks like this, with two rows:
Parts
17
12
If I bind my data source to the grid, the grid automatically just uses the object's properties to create the columns, so I see columns of Label and Value:
Label Value
Parts 17
Parts 12
I am assuming that I cannot achieve what I want just via xaml. Does anyone have an example of what I am looking for? Is it just up to me to create all of the columns during runtime programatically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
和代码:
and code:
Iverzin 的解决方案将与 XamDataGrid 一起使用。它能够自动生成字段,因此您不必在设计时指定它们。
Iverzin's solution will work with the XamDataGrid. It has the ability to autogenerate fields, so you don't have to specify them at design-time.
我在另一篇文章中回答了这样的问题
添加变量列数
,其中我创建了一个行为并根据某些条件添加了列(在我的例子中为字段布局 no)。您可以检查数据源,然后执行相同的操作。
您必须在某处定义一些列集,然后检索它们以基于 data source.ex 为您的 XamDataGrid 创建 FieldLayout。
并在 AddFieldLayout 方法中将字段添加到网格布局中。
I've answered such question in another post
Adding variable no of columns
In which I've created a behavior and added columns dependent on some condition(field layout no in my case). you can check datasource and then do the same.
And you do have to define some sets of columns somewhere and then retrieve them to create a FieldLayout for your XamDataGrid on basis of data source.ex.
And in AddFieldLayout method add fields to layout of grid.