I'm trying to create grid with static and dynamic columns. Dynamic columns should be under ColumnGroups. When I'm not using ColumnGroups everything works ok. But when I try to assign dynamic column to one of groups I get
The calling thread cannot access this object because a different thread owns it.
I create new window in separate thread
Thread newWindowThread=new Thread(new ThreadStart(() =>
{
var instrumentWindow=new InstrumentWindow();
((InstrumentWindowViewModel)instrumentWindow.DataContext).Initialize(instrument.ToString(),DateTime.Today);
instrumentWindow.Show();
System.Windows.Threading.Dispatcher.Run();
}));
newWindowThread.SetApartmentState(ApartmentState.STA);
newWindowThread.IsBackground = true;
newWindowThread.Start();
After I create static columns
this.EventGrid.Columns.Add(new GridViewDataColumn()
{
Header = "Time",
DataMemberBinding = new Binding("Time"),
DataType = typeof(DateTime)
});
And actually function which is responsible for creating dynamic columns
Basically the idea is to create ColumnGroup for each term and under that group display term properties.
private void InitDynamicColumns()
{
var maxTermCount = ((InstrumentWindowViewModel)this.DataContext).Events.Max(x => x.Terms.Length);
var termHeaders = new List<string>();
for (int i = 0; i < maxTermCount; i++)
{
var currentTerm = ((InstrumentWindowViewModel)this.DataContext).Events.First().Terms[i];
var analyzerAmountType = currentTerm.Type == AnalyzerAmountType.Seconds ? "sec" : "ticks";
this.EventGrid.ColumnGroups.Add(new GridViewColumnGroup
{
Header = currentTerm.Size + " " + analyzerAmountType + " term",
Name = currentTerm.Size + analyzerAmountType + "term"
});
termHeaders.Add(currentTerm.Size + analyzerAmountType + "term");
}
for (int i = 0; i < maxTermCount; i++)
{
this.EventGrid.Columns.Add(new GridViewDataColumn()
{
Header = "Property1",
DataMemberBinding = new Binding("Terms[" + i + "].Property1"),
ColumnGroupName = termHeaders[i]
});
this.EventGrid.Columns.Add(new GridViewDataColumn()
{
Header = "Property2",
DataMemberBinding = new Binding("Terms[" + i + "].Property2"),
ColumnGroupName = termHeaders[i]
});
this.EventGrid.Columns.Add(new GridViewDataColumn()
{
Header = "Property3",
DataMemberBinding = new Binding("Terms[" + i + "].Property3"),
ColumnGroupName = termHeaders[i]
});
}
}
When I don't use ColumnGroups everything works as expected.But when I try to assig ColumnGroup to Column I get this Thread exception.
Thanks for any help.