【问题标题】:Bind DataGrid Columns to list将 DataGrid 列绑定到列表
【发布时间】:2014-08-13 19:55:06
【问题描述】:

我正在开发一个基于 MVVM 的 WPF 应用程序。我想将字符串列表绑定到列标题,即,如果列表包含“abc”、“xyz”、“pqr”,那么我的DataGrid 应该具有标题为 abc、xyz、pqr 的三列。这是我绑定数据网格的类。这些行存储在ObservableCollection<List<string>> 中,其中ObservableCollection 的每个元素都是一个字符串列表,它形成了行的单元格。

public class Resource
{
    private ObservableCollection<string> columns;
    public ObservableCollection<string> Columns
    {
        get
        {
            return columns;
        }
        set
        {
            columns = value;
        }

    }

    private ObservableCollection<List<string>> row;
    public ObservableCollection<List<string>> Row
    {
        get
        {
            return row;
        }
        set
        {
            row = value;
        }
    }

    public Resource()
    {
        List<string> a = new List<string>();
        a.Add("1");
        a.Add("2");
        List<string> b = new List<string>();
        b.Add("11");
        b.Add("21");
        Row = new ObservableCollection<List<string>>();
        Row.Add(a);
        Row.Add(b);

        Columns = new ObservableCollection<string>();
        Columns.Add("Hello");
        Columns.Add("World");
    }
}

我在 Internet 上进行了很多搜索,但找不到任何可用的示例。我真的只需要通过这种方法绑定DataGrid

【问题讨论】:

  • 您希望能够对列重新排序吗?
  • @Markus 我需要按列排序,但不需要重新排序
  • 根据我的经验,尽管使用附加属性是可行的,但这种方法过于繁琐且过于局限,并迫使您转向程序代码 UI 特定的事情,而这些事情确实应该在 XAML 中完成。对每个数据类型使用特定的 XAML 定义的 DataTemplate,而不是尝试“一刀切”的解决方案,这种解决方案除了非常基本的纯字符串数据类型外,并不适用于任何东西。

标签: c# wpf mvvm datagrid


【解决方案1】:

您可以通过以下两种方式之一使用 DataGrid:

1) 将 DataGrid 的 ItemsSource 绑定到公开 3 个属性 abc 、 xyz 、 pqr 的元素集合。

CS:

    public List<MyDataItem> DataItems 
    {
        get
        {
            List<MyDataItem> items = new List<MyDataItem>(5);

            for (int i = 0; i < 5; i++)
            {
                items.Add(new MyDataItem { abc = abc[i], qrt = qrt[i], xyz = xyz[i] });
            }

            return items;
        }
    }

    int[] abc = new int[5] { 1, 2, 3, 4, 5 };
    int[] qrt = new int[5] { 6,7,8,9,10 };
    int[] xyz = new int[5] { 11,12,13,14,15};


    public event PropertyChangedEventHandler PropertyChanged = delegate { };

}

public class MyDataItem
{
    public int abc { get; set; }
    public int qrt { get; set; }
    public int xyz { get; set; }
}

XAML:

 <DataGrid ItemsSource="{Binding DataItems}" />    

2) 创建一个 DataTable 对象并将其绑定到您的 ItemsSource。

 public DataTable DataTable
    {
        get
        {
            DataTable table = new DataTable();

            table.Columns.Add("abc");
            table.Columns.Add("qrt");
            table.Columns.Add("xyz");

            table.Rows.Add(1, 6, 11);
            table.Rows.Add(2, 7, 12);
            table.Rows.Add(3, 8, 13);
            table.Rows.Add(4, 9, 14);
            table.Rows.Add(5, 10, 15);

            return table;
        }
    }

【讨论】:

  • 这没有回答问题,它不依赖于Columns,因为MyDataItem 包含固定数量的属性,不依赖于Columns ObservableCollection&lt;string&gt;
猜你喜欢
  • 2013-08-06
  • 1970-01-01
  • 1970-01-01
  • 2015-01-16
  • 2018-10-19
  • 2015-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多