reprinted [wpf][c#] embedded windowsform controls in WPF-using windowsformscontrollibrary

Source: Internet
Author: User

[WPF] [C #] Inline windowsform controls in WPF-using windowsformscontrollibrary

In the article [wpf][c#] embedding windowsform controls in WPF, I'll show you how to embed Windows Form controls directly in WPF, but not everyone likes to write XAML, and sometimes you'll need to put existing windows The form application is embedded in WPF, so this time we'll show you how to use the reference DLL file to add Windows Form from the DLL to WPF.

Have said to use windowsformscontrollibrary, so we will directly build a windowsformscontrollibrary project it!! I then added two Windows Form to the project, Form1 and Form2, then put some controls on it and then built the project.

Then add another WPF application in the scenario as host, and this way I'll just take [wpf][c#] to modify the items in the article "Embedded WindowsForm control in WPF" to change the contents of MainWindow.xaml to read as follows:

MainWindow.xaml

<window x:class= "Wpf_winform.mainwindow" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc= "http://schemas.openxmlformats.org /markup-compatibility/2006 "xmlns:d=" http://schemas.microsoft.com/expression/blend/2008 "Xmlns:wi Nformhost= "Clr-namespace:system.windows.forms.integration;assembly=windowsformsintegration" xmlns:WinFormContr ols= "Clr-namespace:system.windows.forms;assembly=system.windows.forms" mc:ignorable= "D" Title= "Ma Inwindow "D:designwidth=", "d:designheight=", "width=" Auto "height=" Auto "> <grid name=" layou Troot "background=" Gray "> <Grid.ColumnDefinitions> <columndefinition></columndefiniti on> <ColumnDefinition></ColumnDefinition> </Grid.ColumnDefinitions> <wind Owsformshost name= "windowsFormsHost1 "grid.column=" 0 "margin=" 3 "></WindowsFormsHost> <windowsformshost name=" windowsf OrmsHost2 "grid.column=" 1 "margin=" 3 "></WindowsFormsHost> </Grid></Window>

And then we're going to refer to the DLL file that was built by the Windowsformscontrollibrary project in the WPF project (because I have this extension installed productivity Power tools, so add The picture of the reference will look like).

Once referenced, we can play with the Windows Form control in Windowsformscontrollibrary in the WPF project, and here we demonstrate the addition of FORM1 and Form2 to the MainWindow of WPF in C #:

MainWindow.xaml.cs

Using system.windows;using windowsformscontrollibrary;using system.windows.forms;namespace WPF_Winform{Public        Partial class Mainwindow:window {private Form1 _form1 = new Form1 ();        Private Form2 _form2 = new Form2 ();            Public MainWindow () {InitializeComponent (); _form1. TopLevel = false;windowsformshost1.child = _form1;            Panel panelcenter = new Panel {BackColor = System.Drawing.Color.Blue};            Panelcenter.dock = DockStyle.Fill; _form1.            Controls.Add (Panelcenter);  Panel paneltop = new Panel {BackColor = System.Drawing.Color.White, minimumsize = new System.Drawing.Size {Height = 100            , Width = 0}};            Paneltop.dock = Dockstyle.top;            PANELCENTER.CONTROLS.ADD (Paneltop); _form2.            TopLevel = false;        windowsformshost2.child = _form2; }    }}

Take a closer look at the program code above, I added two additional panel controls to Form1, and then look at the results of the execution!!

Hey!!!! Form1 Two panel clearly is later only add in, how will be the original two label covered it?? Because the Z-index and join order of objects in Windows Form is the opposite of WPF (This is important!!). Don't overlook this if you want to play wpf+windows form.

The controls that are first added to the Windows form will overlap, and WPF will overlap the more later. In fact, I put two labels in the Form2, just because I joined two panel, so I will be covered by two panel and not see.

If you look at the contents of the Form2.Designer.cs file, you will find that there is a paragraph like this:

Form2.Designer.cs

This. Controls.Add (THIS.PANEL2); Controls.Add (THIS.PANEL1); Controls.Add (THIS.LABEL2); Controls.Add (THIS.LABEL1);

It's not just adding objects in C #, it's the same as adding Windows Form controls directly in XAML. Oh, the first addition will be on the top!! (a friend who needs to download [wpf][c#] in WPF embedded WindowsForm control's project original file back to play)

Same field as:

What if you want to embed WPF in a ready-made Windows Form application? It's simple to rebuild the project's output type after it's changed to a class library, referencing the DLL file it produced. You can play with it the way it is!

reprinted [wpf][c#] embedded windowsform controls in WPF-using windowsformscontrollibrary

Related Article

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.