Put Wpf Control into a Windows Forms Form

Put WPF control into a Windows Forms Form?

Put an ElementHost control inside the panel. This control can then host a WPF element. From the WinForms designer, you can find this control under 'WPF Interoperability'. First you may need to add WindowsFormsIntegration.dll to your project's references.

For an example, see Walkthrough: Hosting a WPF Composite Control in Windows Forms.

Adding User Control (WPF) to Panel in Windows Form c#

You should be able to use the ElementHost control to wrap the WPF control. See https://msdn.microsoft.com/en-us/library/system.windows.forms.integration.elementhost(v=vs.110).aspx.

private void lblReactiveMaintenance_Click(object sender, EventArgs e)
{
OutlookAddIn.Controls.RForm = new OutlookAddIn.Controls.RForm();
reactiveMaintForm.Name = "rForm";
elementHost2.Child = reactiveMaintForm;
}

Using a custom WPF control in WinForms

Yes you can!

In WinForms use ElementHost and add you WPF control inside (Best practice here is to create a WPF User Control and add you controls into the user control)

And in WPF, if you want to use a WinForms user control, use the WindowsFormsHost

  • WPF Interoperation: "Airspace" and Window Regions Overview
  • Windows Forms and WPF Property Mapping
  • How to: Host a Windows Presentation Foundation Control in Windows Forms by Using ElementHost
  • Walkthrough: Hosting a Windows Forms Control in Windows Presentation Foundation

.Net: How to use a WPF user control in a windows forms application?

From the MSDN:

Use the ElementHost control to place a
WPF UIElement on your Windows Forms
control or form.

Example:

private void Form1_Load(object sender, EventArgs e)
{
// Create the ElementHost control for hosting the
// WPF UserControl.
ElementHost host = new ElementHost();
host.Dock = DockStyle.Fill;

// Create the WPF UserControl.
HostingWpfUserControlInWf.UserControl1 uc =
new HostingWpfUserControlInWf.UserControl1();

// Assign the WPF UserControl to the ElementHost control's
// Child property.
host.Child = uc;

// Add the ElementHost control to the form's
// collection of child controls.
this.Controls.Add(host);
}

Here is a nice tutorial how to do that.

The control called ElementHost is for
WPF in WinForms what the
WindowsFormsHost was for WinForms in
WPF. In the designer, you can find
this control in the Toolbox underneath
"WPF Interoperability".

WPF in Windows Forms

If you find yourself in need to open a WPF Window from a WinForms program:

  1. Create/Add a new project of type "WPF Custom Control Library"
  2. Add a new Item of type "Window (WPF)"
  3. Do your thing with the WPF Window
  4. From your WinForms app, create and open the WPF Window.

code snippet:

WPFWindow.Window1 wpfwindow = new WPFWindow.Window1();
ElementHost.EnableModelessKeyboardInterop(wpfwindow);
wpfwindow.Show();

However ensure you have the following using statements:

using System; //Given 
using System.Windows.Forms; //Given
using System.Windows.Forms.Integration; //Not so Given.

Refer:

How to programmatically create a WPF window in a WinForm application

Open a WPF Window from WinForms & link form app with WPF app

How to add a WPF window to a WinForms App

How to put a custom windows forms control in a WPF application?

You can't really add it as a control to the toolbox like you could for a Windows Forms Application. What you should do instead is "host" the user control inside of the WPF application.

See how to do it on MSDN.

Here's an example of how to use a masked text box (which you can easily modify to use your custom control):

<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="HostingWfInWpf">
<Grid>
<WindowsFormsHost>
<wf:MaskedTextBox x:Name="mtbDate" Mask="00/00/0000"/>
</WindowsFormsHost>
</Grid>
</Window>

Inserting a winform control into WPF

There are many sources available on the subject. Here are a few returned from a simple Google search:

  • Walkthrough: Hosting a Windows Forms Control in WPF
  • Walkthrough: Hosting a Windows Forms Control in WPF by Using
    XAML
  • Mixing WPF and WinForms
  • Mixing WPF with a WinForm application?

I'll explain the first link as it's fairly straightforward and easy to manage. There's actually not much code to it.

First you need to add a reference to the following assemblies:

  • WindowsFormsIntegration
  • System.Windows.Forms

Then create a grid (in this case named Grid1).

Here's the code-behind:

private void Window_Loaded(object sender, RoutedEventArgs e) 
{
// Create the interop host control.
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();

// Create the MaskedTextBox control.
MaskedTextBox mtbDate = new MaskedTextBox("00/00/0000");

// Assign the MaskedTextBox control as the host control's child.
host.Child = mtbDate;

// Add the interop host control to the Grid
// control's collection of child controls.
this.grid1.Children.Add(host);
}

All you're doing is instantiating a WindowsFormsHost then adding a child control named mtbDate. Instantiate your own control and add it using the same method instead. Then just manipulate it as you normally would in Win Forms.

Then add a using at the top of the class:

using System.Windows.Forms;

I hope that helps.

wpf richtextbox in winforms application

This answer is meant to expand on @KyleWang wonderful answer

One BIG issue with Vectors choice of the WPF RichTextBox is There is no Text property in the WPF RichTextBox control. Here is one way to get all of the text out. That said I would IMHO would suggest using the WPF Plain TextBox control

Vector also commented on how to hide the HotReload in the title bar
Tools > Options > Debugging > General > un-check Enable UI Debugging Tools for XAML

OK Code Below hope this is helpful if you decide to use a WPF control in WinForms for spell checking

Public Class frmStart

Dim rtb As Windows.Controls.RichTextBox = New Windows.Controls.RichTextBox()
Dim tb As Windows.Controls.TextBox = New Windows.Controls.TextBox()
Dim str As String = " "

Private Sub frmStart_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ElementHost1.Child = rtb
rtb.SpellCheck.IsEnabled = True

ElementHost2.Child = tb
tb.SpellCheck.IsEnabled = True

If str.Length < 100 Then
rtb.VerticalScrollBarVisibility = Windows.Controls.ScrollBarVisibility.Visible
End If
End Sub

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
str = "Plain WPF TxtBox"
tb.Text = str
rtb.AppendText("Heree is som mispelled txt se if the dictioary wrks more nonsense to see the scroll bar's will this word wrapp or is that rapp")
End Sub

Private Sub btnGet_Click(sender As Object, e As EventArgs) Handles btnGet.Click
Dim elementHost = ElementHost1
Dim wpfRichText = CType(elementHost.Child, Windows.Controls.RichTextBox)
Dim range As Windows.Documents.TextRange = New Windows.Documents.TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd)
Dim allText As String = range.Text
tbMsg.Text = allText.ToString
End Sub

Private Sub btnGTB_Click(sender As Object, e As EventArgs) Handles btnGTB.Click
Dim elementHost = ElementHost2
Dim text = tb.Text
tbMsg.Text = text.ToString
End Sub


Related Topics



Leave a reply



Submit