Infragistics XamDataGrid具有可变列数 [英] Infragistics XamDataGrid with variable number of columns

查看:144
本文介绍了Infragistics XamDataGrid具有可变列数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够支持一个XamDataGrid,它在设计时不会有一组数量的列。例如,应用程序将运行,从服务器获取一些数据并创建一些对象。根据服务器的响应,每次运行应用程序时,我可能会有不同的对象数量。



这是我的意思的一个例子。让我说我打电话给一些服务,并得到一个xml响应与一些信息。我反序列化响应到一些对象,每次调用都可能会有所不同。



让每个对象有两个属性Label和Value。我希望网格显示具有与值值相匹配的标签的列。所以如果我有两个对象obj1和obj2,看起来像这样:

  obj1.Label =Parts
obj1.Value =17

obj2.Label =零件
obj2.Value =12

我想要一个这样的网格,有两行:



零件



17



12



如果我将我的数据源绑定到网格,网格自动只使用对象的属性来创建列,因此我看到了标签和值的列:



标签值



零件17



零件12



我假设我不能通过xaml实现我想要的。有人有我正在寻找的例子吗?在运行时编程时,是否创建所有的列是由我自己决定的?

解决方案

  <网格> 
< DataGrid Name =dgTestAutoGenerateColumns =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();

列表< 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;组; }
public string Value {get;组; }
}


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?

解决方案

 <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; }
}

这篇关于Infragistics XamDataGrid具有可变列数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆