以编程方式向datagrid添加列不进行编译 [英] Programmatically add columns to datagrid not compiling
问题描述
这是继续
任何人都可以解释为什么? p>
---编辑---
我现在意识到我误解了上面的链接。
简而言之,我有一个datagrid绑定到一个observable集合。
我必须再添加两列。如何做?
--- EDIT2 ---- CBreeze
dtgResults.ItemsSource = obcmMyDim;< --------此前的数据
DataGridTextColumn textColumn1 = new DataGridTextColumn();
textColumn1.Header =AAA1;
textColumn1.Binding = new Binding(AAA1);
DataGridTextColumn textColumn2 = new DataGridTextColumn();
textColumn2.Header =AAA2;
textColumn2.Binding = new Binding(AAA2);
Application.Current.Dispatcher.BeginInvoke(new ThreadStart(()=> dtgResults.Columns.Add(textColumn1)));
Application.Current.Dispatcher.BeginInvoke(new ThreadStart(()=> dtgResults.Columns.Add(textColumn2)));
dtgResults.Items.Add(new {AAA1 =Col1Row1,AAA2 =Col2Row1});
dtgResults.Items.Add(new {AAA1 =Col1Row2,AAA2 =Col2Row2});
---编辑3 ---为JH
所以简而言之,绑定到datagrid的集合产生以下输出:
然后我用你的方法添加列:
var names = obcmMyDim.First()。obcItemsName; //所有条目必须具有相同的obcItemsName列表,并且按照相同的顺序
(int i = 0; i< names.Count; i ++)
{
DataGridTextColumn c = new DataGridTextColumn ();
c.Header = names [i];
var b = new Binding();
string str = string.Format(obcmMyDim.obcItemsMeasured [{0}],i);
b.Path = new PropertyPath(str);
b.Mode = BindingMode.TwoWay;
c.Binding = b;
dtgResults.Columns.Add(c);
}
对于绑定数组
,bind str是obcmMyDim.obcItemsMeasured [0]... 1 .... n
但是我得到的是列是在那里,但它们是空的
解决方案你有没有尝试像
dtgResults.Columns.Add(new DataGridTextColumn {标题=a.1});
dtgResults.Columns.Add(new DataGridTextColumn {Header =a.2});
编辑:
(int i = 1; i< = 10; i ++)
{
dtgResults.Items.Add(new {a.1 =Test + i,a.2 =Test+ i});
}
This is the continuation to
I have seen this post:
the datagrid is simply this:
<DataGrid Name="dtgResults" Background="Transparent" AutoGenerateColumns="False"/>
in which everything seems to work fine but when I put it in my solution it doesn't compile:
Can anyone explain me why?
---EDIT---
I realize now that I have misunderstood what's in the link above. In short I have a datagrid binded to an observable collection. I have to add two more columns. How can that be done?
---EDIT2---- for CBreeze
dtgResults.ItemsSource = obcmMyDim;<--------previous data here DataGridTextColumn textColumn1 = new DataGridTextColumn(); textColumn1.Header = "AAA1"; textColumn1.Binding = new Binding("AAA1"); DataGridTextColumn textColumn2 = new DataGridTextColumn(); textColumn2.Header = "AAA2"; textColumn2.Binding = new Binding("AAA2"); Application.Current.Dispatcher.BeginInvoke(new ThreadStart(() => dtgResults.Columns.Add(textColumn1))); Application.Current.Dispatcher.BeginInvoke(new ThreadStart(() => dtgResults.Columns.Add(textColumn2))); dtgResults.Items.Add(new { AAA1 = "Col1Row1", AAA2 = "Col2Row1"}); dtgResults.Items.Add(new { AAA1 = "Col1Row2", AAA2 = "Col2Row2" });
---EDIT 3--- for JH So in short I have that observable collection which binded to the datagrid make the following output:
then I add the columns with your method:
var names = obcmMyDim.First().obcItemsName; // All entries must have the same list of obcItemsName and in the same order for (int i = 0; i < names.Count; i++) { DataGridTextColumn c = new DataGridTextColumn(); c.Header = names[i]; var b = new Binding(); string str = string.Format("obcmMyDim.obcItemsMeasured[{0}]", i); b.Path = new PropertyPath(str); b.Mode = BindingMode.TwoWay; c.Binding = b; dtgResults.Columns.Add(c); }
as for the binded array
and the bind str is "obcmMyDim.obcItemsMeasured[0]" ...1....n
but what I get is that the columns are there but they are empty
解决方案Have you tried something like;
dtgResults.Columns.Add(new DataGridTextColumn { Header = "a.1"}); dtgResults.Columns.Add(new DataGridTextColumn { Header = "a.2"});
EDIT:
for (int i = 1; i <= 10; i++) { dtgResults.Items.Add(new { a.1 = "Test" + i, a.2 = "Test" + i}); }
这篇关于以编程方式向datagrid添加列不进行编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!