banner



How To Use Wpf Control In Windows Form In C#

  • Updated date Aug 01, 2011
  • 222.5k
  • 0

In this tutorial, you will learn how to use existing Windows Forms controls in a WPF application. I take used Visual Studio 2008 to create this tutorial.

In this tutorial, you volition learn how we tin can use existing Windows Forms controls in a WPF application. I accept used Visual Studio 2008 to create this tutorial.

Note: If yous do not have Visual Studio 2008, y'all may want to download Visual Studio 2008 Express versions hither: http://world wide web.microsoft.com/limited/default.aspx . All Express versions are free.

Step 1: Create a WPF Application using Visual Studio 2008

Get-go create a WPF application using Visual Studio 2008. Click on File >> New >> Project menu and select WPF Awarding from Templates as shown in Figure 1.

WinFormToWPFImg1.jpg

Figure 1. Create a WPF Application

Step ane: Add together Reference to Windows FormsIntegration and Windows Forms namespaces

In Solution Explorer, right click on References node and select Add Reference menu item. On Browse tab, get to "C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0" folder and select WindowsFormsIntegration.dll file and click OK button. If you accept .NET 3.5 installed, you should likewise see v3.5. Y'all need to select the electric current version of DLL y'all are working with. Run into Effigy 2.

WinFormToWPFImg2.jpg

Effigy 2. Calculation WindowsFormsIntegration reference

Select Add Reference again and this time on .Net tab, select System.Windows.Forms and click OK. See Effigy 3.

WinFormToWPFImg3.jpg

Effigy iii. Adding System.Windows.Forms reference

This activeness adds assemblies to your application (copies to the Bin folder) you need to work with Windows Forms controls in your WPF application.

Next, go to your XAML file and add the following namespace and assembly reference within the Windows tag.

xmlns : wf ="clr-namespace:System.Windows.Forms;assembly=Arrangement.Windows.Forms"

The final Windows tag looks similar this:

< Window 10 : Course ="WinFormInWPF.Window1"

xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns : ten ="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns : wf ="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"

Championship ="Window1" Pinnacle ="300" Width ="300" Loaded ="Window_Loaded">

< Grid >

</ Grid >

</ Window >

Step 2: Add Windows Forms control to XAML

There are 2 ways to create a Windows Forms command in XAML. Either we can create a Windows Forms command in our XAML code using XAML syntaxes or nosotros can create at runtime past using Windows Forms classes.

Showtime, let's come across how we can create a Windows Forms control in XAML.

Say, you want to use Windows Forms' ListBox command in your WPF application, yous merely have to add together the beneath code inside your Grid tag in XAML. The WindowsFormsHost tag is used to host Windows Forms control and wf:ListBox tag represents the Windows Forms ListBox. Similarly, you can add whatever Windows Forms control to your WPF application.

< Grid >

< WindowsFormsHost >

< wf : ListBox x : Name ="lstBox"/>

</ WindowsFormsHost >

</ Grid >

Footstep 3: Add Windows Forms control support to code

Subsequently that you need to get Window1.xaml.cs file to import these namespaces so nosotros can apply Windows Forms and related classes.

using System.Windows.Forms;

using System.Windows.Forms.Integration;

Step 4: Using Windows Forms control

At present, we tin can use Windows Forms command in our .cs file every bit in previous Windows Forms applications. If you double click on the Window in your XAML designer, information technology will add together Window_Loaded consequence handler to the .cs file.

In the following code, I access lstBox control on Windows_Loaded event handler and calls Items.Add method a few times to add items to the ListBox.

private void Window_Loaded(object sender, RoutedEventArgs due east)

{

lstBox.Items.Add together("Mahesh Chand");

lstBox.Items.Add("XAML Title");

lstBox.Items.Add together("JJ Kohls");
}

Step 5: Build and Run

Now build and run your awarding. The output is Figure 4.

WinFormToWPFImg4.jpg

Figure 4. Windows Forms' ListBox in a WPF Application

Tip 1: Using multiple Windows Forms Controls

If you wish to use multiple Windows Forms controls in a WPF application, each control should exist inside a WindowsFormsHost tag.

< WindowsFormsHost >

< wf : ListBox x : Name ="lstBox"/>

</ WindowsFormsHost >

< WindowsFormsHost >

< wf : DataGridView x : Proper noun ="DbGrid" />

</ WindowsFormsHost >

Tip 2: Using Windows Forms Controls Dynamically

In previous steps, we created a ListBox control in XAML. You may likewise create the control dynamically using Windows Forms classes. The post-obit code creates a WindowsFormsHost object, a ListBox control and adds the ListBox control to the host. In the end, the host s added to the XAML Grid.

private void Window_Loaded(object sender, RoutedEventArgs due east)

{

WindowsFormsHost host = new WindowsFormsHost();

//Create a Windows Forms ListBox control

System.Windows.Forms.ListBox lstBox = new System.Windows.Forms.ListBox();

lstBox.Items.Add("Mahesh Chand");

lstBox.Items.Add together("XAML Title");

lstBox.Items.Add together("JJ Kohls");

// Add the Windows Forms ListBox to the host control

host.Kid = lstBox;

// Add the host control to the WPF chemical element that yous want to parent the control,

// in this case information technology's a Grid

this.Grid1.Children.Add together(host);

}

Summary

In this article, I discussed how we can utilise a Windows Forms' ListBox controls in WPF applications. You may use same approach to use any Windows Forms control in WPF such every bit DataGrid or DataGridView controls.

How To Use Wpf Control In Windows Form In C#,

Source: https://www.c-sharpcorner.com/uploadfile/mahesh/using-windows-forms-controls-in-wpf/

Posted by: yearwoodlifeare.blogspot.com

0 Response to "How To Use Wpf Control In Windows Form In C#"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel