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
You can bind to an item in an array but you'll have to make sure that all the arrays are the same (same fields, in the same order so that the index into them is consistent). Here is an example of it based on the other question you had.
Note that the field obcItemsMeasured had to be changed into a property since bindings require properties (not fields).
XAML:
Code:
Screenshot:
Have you tried something like;
EDIT:
It does not compiling because you use
ItemCollection
. I think you did not create your own class like in answer from link and usedSystem.Windows.Controls.ItemCollection
, which can`t used in this case.