【问题标题】:DataGridColumn Binding with Dynamically Generated DataDataGridColumn 与动态生成的数据绑定
【发布时间】:2012-06-17 16:32:06
【问题描述】:

我的程序中有大量从 Web 服务生成的数据。我需要在 DataGrid 中显示这些数据,但返回的数据集合都有不同的格式(一个可能是 3 列,接下来是 7 列),因此我无法创建特定对象来保存这些数据。我无法显示此信息。现在我正在尝试使用这种方法。我将数据放入 KeyValuePairs 的二维列表中。键是该数据所属的列,值是该行的输出。这就是我现在正在尝试的。

private void SetDataValuesExecute(List<List<KeyValuePair<string,object>>> Data)
    {
        ResultsCollection = Data;
        ValuesDataGrid.Columns.Clear();
        foreach (string colName in Data[0].Select(x => x.Key).ToList())
        {
            DataGridTextColumn tempCol = new DataGridTextColumn();
            tempCol.Header = colName;
            Binding bind = new Binding();
            bind.Source = ResultsCollection.Select(x => x.FirstOrDefault(y => y.Key == colName).Value.ToString()).ToList();
            tempCol.Binding = bind;
            ValuesDataGrid.Columns.Add(tempCol);         
        }
    }

ResultsCollection 是一个非易失性变量,用作我的绑定源。它是我构建 DataGrid 所需的所有二维列表数据的副本。

这会查看第一个条目,然后提取列标题值并基于该值创建新列。绑定语句查看每一行,获取指定列的数据,并尝试将其设置为列的绑定。这使我可以同时拥有列名和该列中所有数据的列表。不幸的是,将数据列表绑定到列最终不会显示任何数据。

所以我的问题是:我需要如何格式化我的数据才能让列绑定实际显示数据?我接近这种情况完全错了吗?感谢任何帮助,因为我很困惑。

【问题讨论】:

  • 如果你只通过一行是否有效?可以肯定的是,它的行列数少于第一列,并抛出和超出范围异常。我认为您将需要遍历行以找到最大 col 计数,然后将每一行填充到该最大计数。
  • @Blam 每个单独数据集的所有行都具有完全相同的字段(相同的列数)。我得到一组新的数据来更改列号,当发生这种情况时,我将清除 DataGrid 并用新数据替换它。

标签: c# wpf dynamic binding datagrid


【解决方案1】:

这是一个完整的工作示例

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

        var data = new MyViewModel();

        int columnIndex = 0;

        foreach (var name in data.ColumnNames)
        {
            grid.Columns.Add(
                new DataGridTextColumn
                {
                    Header = name,
                    Binding = new Binding(
                        string.Format("Values[{0}]", columnIndex++))
                });
        }

        DataContext = data;
    }
}

public class MyViewModel
{
    public MyViewModel()
    {
        Items = new List<RowDataItem>
            {
                new RowDataItem(),
                new RowDataItem(),
                new RowDataItem(),
            };

        ColumnNames = new List<string>{ "Col 1", "Col 2", "Col 3"};
    }

    public IList<string> ColumnNames { get; private set; }

    public IList<RowDataItem> Items { get; private set; }
}

public class RowDataItem
{
    public RowDataItem()
    {
        Values = new List<string>{ "One", "Two", "Three"};    
    }

    public IList<string> Values { get; private set; }
}

Xaml

<Grid>
    <DataGrid x:Name="grid" ItemsSource="{Binding Items}" 
              AutoGenerateColumns="False"/>
</Grid>

【讨论】:

  • 我尝试设置它,但它仍然没有显示任何数据。为了澄清起见,我将 DataValuesGrid 的 DataContext 设置为 Items 列表。这应该公开我通过添加Binding = new Binding(string.Format("Values[{0}]", columnIndex++)) 的绑定访问的 Values 属性。每个“Values”都包含一个表示每列数据的字符串列表(列索引与行中的项目索引匹配)。列标题当然显示得很好,但列本身似乎没有从列中获取任何数据。我需要更改绑定的内容吗?
  • @user1456624:已更新,希望对您有所帮助。
  • (更新了我的用户名)即使在 ItemsSource 更改后仍然无法正常工作。 Items 的结构是这样的 => Items[index].Values[index] 包含我要输出的值的字符串表示形式。因此,Items 的索引将您带到每个 RowDataItem,而 Values 中的索引使您可以访问每个单独的值。这是正确的,对吧? ItemsSource = Items,DataContext 是我的 ViewModel,第 1 列的绑定应该是 Values[0],第 2 列的绑定应该是 Values[1],依此类推。我觉得一切都正确连接,但没有运气。感谢所有的帮助。
【解决方案2】:

这是一个 GridView,但它可以工作。我认为您需要使用 bind.Path 但 KVP 不确定其余的语法。

WPF - Display Grid of Results With Dynamic Columns/Rows

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 2011-04-07
    • 2019-03-09
    • 2023-04-02
    • 2018-11-16
    • 1970-01-01
    相关资源
    最近更新 更多