Joining WPF controls in WinForm and binding data sources for automatic Updates across threads

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.