具有可变列数的 Infragistics XamDataGrid

发布于 2024-11-03 04:46:25 字数 651 浏览 1 评论 0原文

我需要能够支持 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情话墙 2024-11-10 04:46:25
 <Grid>
    <DataGrid Name="dgTest" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.HeaderTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=ItemsSource[0].Label}" />
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.HeaderTemplate>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=Value}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

和代码:

public partial class Window12 : Window
{
    public Window12()
    {
        InitializeComponent();

        List<MyClass> l = new List<MyClass>();

        l.Add(new MyClass
        {
            Label = "Parts",
            Value = "17"
        });

        l.Add(new MyClass
        {
            Label = "Parts",
            Value = "12"
        });

        dgTest.ItemsSource = l;
    }
}

public class MyClass
{
    public string Label { get; set; }
    public string Value { get; set; }
}
 <Grid>
    <DataGrid Name="dgTest" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.HeaderTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=ItemsSource[0].Label}" />
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.HeaderTemplate>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=Value}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

and code:

public partial class Window12 : Window
{
    public Window12()
    {
        InitializeComponent();

        List<MyClass> l = new List<MyClass>();

        l.Add(new MyClass
        {
            Label = "Parts",
            Value = "17"
        });

        l.Add(new MyClass
        {
            Label = "Parts",
            Value = "12"
        });

        dgTest.ItemsSource = l;
    }
}

public class MyClass
{
    public string Label { get; set; }
    public string Value { get; set; }
}
白况 2024-11-10 04:46:25

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.

柏林苍穹下 2024-11-10 04:46:25

我在另一篇文章中回答了这样的问题

添加变量列数

,其中我创建了一个行为并根据某些条件添加了列(在我的例子中为字段布局 no)。您可以检查数据源,然后执行相同的操作。

您必须在某处定义一些列集,然后检索它们以基于 data source.ex 为您的 XamDataGrid 创建 FieldLayout。

        XamDataGrid xamDataGrid;
        if (DataSource.GetType() == typeof(X))
        {
            AddFieldLayout1(xamDataGrid);
        }
        else if (DataSource.GetType() == typeof(Y))
        {
            AddFieldLayout2(xamDataGrid);
        }

并在 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.

        XamDataGrid xamDataGrid;
        if (DataSource.GetType() == typeof(X))
        {
            AddFieldLayout1(xamDataGrid);
        }
        else if (DataSource.GetType() == typeof(Y))
        {
            AddFieldLayout2(xamDataGrid);
        }

And in AddFieldLayout method add fields to layout of grid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文