如何以编程方式形成可观察的集合并将其绑定到数据网格 [英] How to programmatically form an observable collection and bind it to a datagrid

查看:157
本文介绍了如何以编程方式形成可观察的集合并将其绑定到数据网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我会分两步:

I have a problem which i'd divide in two steps:

问题1。

形成一个可观察的字符串集合,并将其绑定到datagrid

Form an observable collection of strings and bind it to a datagrid

ObservableCollection<string[]> octest = new ObservableCollection<string[]>();
var el = new string[6] {"1","2","3", "4", "5", "6" };
octest.Add(el);
octest.Add(el);
octest.Add(el);
dtgResults.ItemsSource = octest;

问题是绑定结果如下图:

the problem is that the binding results in the image below:

问题2

相同但现在有一个字符串数组,但是混合元素(例如3个字符串和3个双精度)

The same but now with an array of strings but with mixed elements (e.g. 3 strings and 3 doubles)

谢谢

- 编辑CBreeze

--EDIT-- for CBreeze

我已经做到了: p>

I have done that:

  ObservableCollection<TestModel> t = new ObservableCollection<TestModel>();
  var t1 = new TestModel();
  t.Add(t1);
  t.Add(t1);
  t.Add(t1);
  dtgResults.ItemsSource = t;
 }
}

public class TestModel
{
  public TestModel()
  {
    TestDouble1 = new string[3];
    TestDouble1[0] = "A";
    TestDouble1[1] = "B";
    TestDouble1[2] = "C";
  }
  public string[] TestDouble1 { get; set; }
}

,结果是:

推荐答案


以编程方式添加列&行到WPF Datagrid

你可以这样做:

string[] columnLabels = new string[] { "Column 0", "Column 1", "Column 2", "Column 3", "Column 4", "Column 5" };

foreach (string label in columnLabels)
{
 DataGridTextColumn column = new DataGridTextColumn();
 column.Header = label;
 column.Binding = new Binding(label.Replace(' ', '_'));

 dtgResults.Columns.Add(column);
}

int[] ivalues = new int[] { 0, 1, 2, 3 };
string[] svalues = new string[] { "A", "B", "C", "D" };

dynamic row = new ExpandoObject();

for (int i = 0; i < 6; i++)
{

 switch (i)
 {
  case 0:
  case 1:
  case 2:
   string str = columnLabels[i].Replace(' ', '_');
   ((IDictionary<String, Object>)row)[str] = ivalues[i];
   break;

   case 3:
   case 4:
   case 5:
    string str2 = columnLabels[i].Replace(' ', '_');
    ((IDictionary<String, Object>)row)[str2] = svalues[i - 3];
    break;

 }
}

dtgResults.Items.Add(row);

这篇关于如何以编程方式形成可观察的集合并将其绑定到数据网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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