1. Add two ElementHost in winform, a button for wpf, and a textbox for WPF. Where the textbox is bound to the data source, the implementation can also be automatically updated across threads, without the presence of a WinForm textbox control that is bound to the data source, where an error occurs in the Update data source (cross-thread update Control) in the child Thread.
usingSystem;usingSystem.Collections.Generic;usingsystem.componentmodel;usingsystem.data;usingsystem.drawing;usingsystem.linq;usingsystem.text;using system.windows; //add support for WPF controls yourselfusing System.Windows.Data; //add support for WPF controls yourselfusing System.Windows.Controls; //add support for WPF controls yourselfusingSystem.Windows.Forms;usingSystem.IO.Ports;usingsystem.net;usingSystem.Net.Sockets;usingsystem.threading;namespaceui{ public Partial classMainform:form { public classdatasource:inotifypropertychanged {Private int_index; public intIndex {Get{return_index;} Set{_index=value; if(propertychanged! =NULL) { this. Propertychanged.invoke ( this,NewPropertyChangedEventArgs ("Index")); } } } public EventPropertyChangedEventHandler propertychanged; } System.Windows.Data.Binding _bind; Thread _thread; DataSource _datasource; BOOL_run; publicmainform () {initializecomponent (); //Create a WPF Button System.Windows.Controls.ButtonBTN =NewSystem.Windows.Controls.Button (); Btn. Content="Button in WPF";//Modify Content PropertiesSystem.Windows.Media.FontFamily Font =NewSystem.Windows.Media.FontFamily ("Ariel");//Modify Font PropertiesBtn. FontFamily =font; Btn. Click+=NewSystem.Windows.RoutedEventHandler (btn_click);//Increase Incident Response//Add it to ElementHostElementhost1.child =btn; //Create a WPF TextBox System.Windows.Controls.TextBoxTxtbox =NewSystem.Windows.Controls.TextBox (); Txtbox.text="TextBox in WPF";//Modify Content Propertiestxtbox.fontfamily =font; _datasource=NewDataSource (); //System.Windows.Data.Binding Way_bind =NewSystem.Windows.Data.Binding (); _bind. Source=_datasource; _bind. Path=NewPropertyPath ("Index"); _bind. UpdateSourceTrigger=updatesourcetrigger.propertychanged; Txtbox.setbinding (System.Windows.Controls.TextBox.TextProperty, _bind); //adding data source bindings//Add it to ElementHostElementhost2.child =txtbox; //Child Threads Run_run =true; _thread=NewThread (Test); _thread. Start (); } voidTest () { while(_run) {_datasource.index++; Thread.Sleep ( -); } } Private voidBtn_click (Objectsender, EventArgs E) {System.Windows.Forms.MessageBox.Show ("WPF button clicked!"); } Private voidMainform_formclosing (Objectsender, FormClosingEventArgs E) {_run=false; if(_thread! =NULL) {_thread. Join (); _thread=NULL; } } }}
Joining WPF controls in WinForm and binding data sources for automatic Updates across threads