Solved

Working WPF Window into a WinForms (VS 2005) Project

Posted on 2011-03-02
6
748 Views
Last Modified: 2013-12-17
I know this is weird and not an ideal approach but stick with me here. :)

So I have a project that I made in VS 2005. In the newest build I want to integrate a WPF window with it, so I designed the window in VS 2008 and compiled it as a library. I then included that lib in the VS 2005 project, along with PresentationCore, PresentationFramework, and WindowsBase. I can call the WPF window from the class library and the code behind the buttons on the window works, but user's can't type in the textboxes.

The controls on the WPF window respond to mouse events. They highlight on mouseover. As mentioned, if a user clicks the button on the window, it runs the code behind it as expected. Clicking on a textbox on the WPF window puts the focus on the textbox and the cursor is displayed, but no text appears to enter the textbox when typing.

Any ideas how to make that work?


EDIT: Another observation -- I can cut/copy from and paste into the textbox. But it still doesn't allow the typing of any characters. When I paste text into the textbox, I can also backspace/delete characters in it. The arrow keys do not allow me to move the cursor forward or backward through any text that I've pasted into the box.
0
Comment
Question by:elorc
  • 3
  • 3
6 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 35022086
Does the machine have .Net Framework 3.X on it?
0
 
LVL 1

Author Comment

by:elorc
ID: 35022873
Yes, for now I'm actually just only testing it on the development machine that I have which runs both VS 2005 and VS 2008. I have .NET Framework 3.5 SP1 installed.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 35027793
Hi elorc;

I found this on a Microsoft site


Q: How do I host a WPF control in a Windows Forms application?

A: First add references to the WPF namespaces (PresentationCore, PresentationFramework, UIAutomationProvider, UIAutomationTypes, and WindowsBase). Next create an instance of the ElementHost control and the control you wish to embed in the Windows Forms application and then hook that control up to the ElementHost control. Then simply add the ElementHost control to your Forms control collection:

ElementHost host = new ElementHost();

System.Windows.Controls.ListBox wpfListBox = new System.Windows.Controls.ListBox();

for (int i = 0; i < 10; i++)
{
    wpfListBox.Items.Add("Item " + i.ToString());
}

host.Dock = DockStyle.Fill;
host.Controls.Add(wpfListBox);
this.panel1.Controls.Add(host);

However, if you want to use XAML to describe the WPF control that you want to use in the Windows Forms application, you would need to add an Avalon UserControl item to your project. This will create a UserControl1.xaml file and a UserControl1.xaml.cs file. You can then modify the UserControl1.xaml file to contain whatever XAML you wish to describe your control. Then you would simply create an instance of this control and add it to the ElementHost control as in the above example:

ElementHost host = new ElementHost();
UserControl1 uc1 = new UserControl1();
host.Controls.Add(uc1);
host.Dock = DockStyle.Fill;
this.panel1.Controls.Add(host);

In addition, you will need to modify the project file because the Windows Application does not what to do with the XAML file. You will need to open the project file (.csproj, .vbproj, etc.) in an editor like Notepad and then scroll to the bottom. You will see the following line:

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

You will need to copy this line and paste it just below the above line and then change "CSharp" to "WinFX" so that the two lines look like:

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

<Import Project="$(MSBuildBinPath)\Microsoft.WinFx.targets" />

Now save this file and reload the project using VS and run the application.

Fernando
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 62

Expert Comment

by:Fernando Soto
ID: 35027809
BTW the reference to that web site is Windows Forms – WPF Interoperability FAQ
0
 
LVL 1

Accepted Solution

by:
elorc earned 0 total points
ID: 35028146
I actually figured this out. I needed to include a reference to WindowsFormsIntegration in the WinForms project. Then, when I loaded the WPF window in the WinForms project, I used this code:

MyWPFWindow x = new MyWPFWindow();
ElementHost.EnableModelessKeyboardInterop(x);
x.Show();

Open in new window


Compiled and ran it, and as Charlie Sheen says, WINNING! :)
0
 
LVL 1

Author Closing Comment

by:elorc
ID: 35067769
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now