【问题标题】:Infragistics XamDataGrid with variable number of columns具有可变列数的 Infragistics XamDataGrid
【发布时间】:2011-04-25 23:40:16
【问题描述】:

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

这是我的意思的一个例子。可以说我调用了一些服务并返回一个带有一些信息的 xml 响应。我将该响应反序列化为多个对象,每次调用时这些对象都可能不同。

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

obj1.Label = "Parts"
obj1.Value = "17"

obj2.Label = "Parts"
obj2.Value = "12"

我想要一个看起来像这样的网格,有两行:

零件

17

12

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

标签值

第 17 部分

第 12 部分

我假设我无法仅通过 xaml 实现我想要的。有没有人有我正在寻找的例子?是否只能由我在运行时以编程方式创建所有列?

【问题讨论】:

    标签: wpf data-binding infragistics xamdatagrid


    【解决方案1】:
     <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; }
    }
    

    【讨论】:

    • 对不起,如果我在原始帖子中不清楚。我正在寻找使用 Infragistics XamDataGrid 的解决方案。
    • 此解决方案如何帮助添加可变数量的列?
    【解决方案2】:

    Iverzin 的解决方案适用于 XamDataGrid。它具有autogenerate fields 的功能,因此您不必在设计时指定它们。

    【讨论】:

      【解决方案3】:

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

      Adding variable no of columns

      我在其中创建了一个行为并添加了依赖于某些条件的列(在我的情况下是字段布局)。您可以检查数据源,然后执行相同操作。

      您必须在某处定义一些列集,然后检索它们以根据数据源.ex 为您的 XamDataGrid 创建一个 FieldLayout。

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

      并在 AddFieldLayout 方法中将字段添加到网格布局中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多