Today is ready to do a login interface, want to use WPF to make a more beautiful interface, all in the WinForm project to join the WPF user control, the process is as follows, Frmlogin is the WinForm interface, is the parent form, The UserControl of Login.xaml WPF is a subform,
First open the Frmlogin design interface, in the left side of the toolbar to find the drag to the interface, and the dock is set to fill, in fact, let ElementHost fill the entire interface. Then add the following code in the Frmlogin:
1 protected Override voidOnLoad (EventArgs e)2 { 3 if(!DesignMode)4 {5 varLogin =NewLogin ( This);6Elementhost1.child =login; 7 }8 9 Base. OnLoad (e);Ten}
Here is an example, I am in this UserControl click Login, hide themselves at the same time, also need to hide the parent form, it will hide the frmlogin, so I would like to set a property in the parent form code: myopacity is readable and writable. The code is as follows:
Public Doublemyopacity{Get{return This. Opacity; } Set{ This. opacity=value;}}protected Override voidOnLoad (EventArgs e) {if(!DesignMode) { varLogin =NewLogin ( This);//Here the parent form is passed to the subform login to Elementhost1.child=login; } Base. OnLoad (e);}
Then add the following in the subform code:
PublicLogin (frmlogin frm): This()//by using the constructor to fetch Frmlogin, which is the parent form, to { This. Frmlogin =frm; Trepository=Newt_huntersrepository ();}Private voidBtnstart_onclick (Objectsender, RoutedEventArgs e) { if(Trepository.login (Username.text, Userpassword.text)) {Form1 Form1=NewForm1 (); Form1. Show (); This. Opacity =0; Change your transparency frmlogin.myopacity=0; Change the transparency of the parent form here}Else{MessageBox.Show ("Check your username and password! ","system prompt:"); } }
Simple interface, green part for WPF UserControl, red for WinForm form, click Login successful, both will be hidden.
Reprint Please specify: "Handsome hedgehog classroom", if you have any questions QQ contact: 363608715
The UserControl of WPF in the WinForm of "the Handsome Hedgehog class"