I have a scenario, in witch i need to open new WPF window form Web Forms page, and ten bind to properities in that window and display it in update panel, so the data on web page, changes with user input in WPF window.
I tried something like this:
public partial class WorkerPanel : System.Web.UI.Page
{
private MainWindow _mainWindow;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
_mainWindow = new MainWindow();
_mainWindow.Show();
}
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
rptTransactions.DataSource = _mainWindow.Distributors;
rptTransactions.DataBind();
}
}
But it gives me the following error:
The calling thread must be STA, because many UI components require this.
Accoridingly to this question https://stackoverflow.com/questions/2329978 i changed my code to:
Thread t = new Thread(() =>
{
_mainWindow = new MainWindow();
_mainWindow.Show();
System.Windows.Threading.Dispatcher.Run();
});
t.SetApartmentState(ApartmentState.STA);
t.IsBackground = true;
t.Start();
With this, my web page and WPF window loads fine, but i cannot bind to this window properties, as it runs in new thread. Is such binding even possible or I should take different approach ?