0
votes

I have a WinForms application that acts as a service(so no actual UI), but it is a WinForms application.
In this application I a form that has an ElementHost and in the elementHost I have a WPF grid.
In one of my classes I have this code:

Dim frm1 As New frmHostWpfGrid '->HERE I get the error

frm1.WpfGrid.WpfGridControl.BeginDataUpdate()
frm1.WpfGrid.WpfGridControl.ItemsSource = Nothing
frm1.WpfGrid.WpfGridControl.RestoreLayoutFromStream(LayoutStream)
frm1.WpfGrid.WpfGridControl.ItemsSource = mdtData
frm1.WpfGrid.WpfGridControl.EndDataUpdate()

THe error I get is:
The calling thread must be STA, because many UI components require this.
When I tried with a WinForms form and a grid I didn't have this problem.

PS1. Notice that I don't want to show the form
PS2. I tried to fill directly a WPF grid without a form and I got the same error

1

1 Answers

-2
votes

You are trying to create UI objects on a non UI thread. Its hard to tell exactly what is causing the error without seeing your form initialization code where it must be setting up some of the WPF elements.

I will need to know more about frmHostWpfGrid to give you an exact answer.