שתף באמצעות


Inserting a WPF User Control in Windows Form.

Question

Saturday, November 17, 2012 11:01 AM

Hi! I am Super Recoder.

I am first time trying to use a Windows Presentation Foundation User Control in a Simple Windows Form Application. Problem is that I can't insert it right.

Steps I did :-

1) Open Microsoft Visual Studio 2008 SP1.

2)Create a New Project --> Visual Basic --> Windows Form Application.

3)Add New Item --> User Control (WPF)

4)Designed my WPF Control, and went back to my Form1.

5) Saved my Project.

6)Successfully I built my Project.

7)Added the ElementHost from the ToolBox (since I did not see anything other that could inter operate WPF Control.) and in the Select Hosted Content, I was surprised to see 'None'.

8) I builded the Application Several times but nothing happened. I guess Visual Studio is not able to recognize the WPF UserControl Somehow.

It's the same with C#. I don't think its a Visual Studio error since, i saw many tutorial doing that properly. If I am missing any steps, please correct me.

I will be thankful to you.

Regards,

Super Recoder

All replies (16)

Monday, November 19, 2012 7:24 AM ✅Answered

I builded my solution after adding the wpf control. Problem is that the Element Host does not recognize the wpf control.

By the Way, I found an alternative, I coded and new Element Host and added the Host Child as the WPF Control.

Private WithEvents WPFControl as new UserControl1

Private Sub Form1_Load() Handles Me.load
  Dim Host as new ElementHost
  Host.child = WPFControl
  Me.Controls.Add(Host)
End Sub

Saturday, November 17, 2012 11:14 AM | 1 vote

Windows.Forms and WPF are two distinct toolkits for building applications. While combining them is possible, you should have a very specific need before undertaking this. Are you building a desktop application (for which Windows.Forms is designed) or a light-weight client for use on the web or a phone(better suited to WPF)?

There are only a few common controls that don't exist in roughly similar form in both toolkits.

"Premature optimization is the root of all evil." - Knuth

If I provoked thought, please click the green arrow

If I provoked Aha! please click Propose as Answer


Saturday, November 17, 2012 11:15 AM | 1 vote

Did you check out your toolbox for the user control ?


Saturday, November 17, 2012 11:21 AM

My necessity of using WPF is that I found it way easier to code a particular object in WPF and I can't do it in Windows Forms. (probably I am not that expert in handling/coding everything)

User Control?? I can find any such thing there. Only Tab in toolbox (related to WPF) is the WPF  Interoperability which has the Element Host control.

I read from Microsoft Help to draw an instance of the UserControl1 in the Form but I don't see that thing. I tried reset my toolbox too.


Saturday, November 17, 2012 11:33 AM | 1 vote

when you build a wpf usercontrol and import system.xaml into your windows forms app then you can see your usercontrol in your toolbox

IMAGE


Saturday, November 17, 2012 11:35 AM | 1 vote

when you rebuild your app you see the wpf user control in the toolbox  then in the desinger just drag it on your winform


Saturday, November 17, 2012 11:40 AM

Yeah! Microsoft Help also said the same thing but the problem is that the ToolBox doesn't have that tool instance.

There is no such user control tool after I rebuild my app. I am thinking it is a problem with the MS VB 2008 Settings? I tried resetting my settings too.


Saturday, November 17, 2012 11:45 AM

I don't see any System.Xaml in my reference, nor I can see it in my Add Reference box. There is the System.xml present.


Saturday, November 17, 2012 11:46 AM | 1 vote

In your references tab in your project do you have the system.xml reference


Saturday, November 17, 2012 11:50 AM | 1 vote

I don't see any System.Xaml in my reference, nor I can see it in my Add Reference box. There is the System.xml present.

Click add new reference click browse navigate to 

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\NETFramework\v4.0

and try adding these presentationcore , presentationframework and system.xaml


Saturday, November 17, 2012 11:51 AM

I have the System.xml not the System.Xaml.

I clicked Add --> .NET Tab --> Searched for System.Xaml but could not find that.


Saturday, November 17, 2012 11:59 AM | 1 vote

I am using vs 2010 maybe its a little diffrent on your side with vs 2008 but if i dont add the system.xaml reference a can not build my winforms app without errors  


Saturday, November 17, 2012 12:03 PM

I am using Microsoft Visual Studio 2008 and it does not support .NET Framework v4.0.

It's limited upto 3.5 and hence no System.Xaml.

But people do build Applications integrating WPF with Forms using perhaps the ElementHost.


Sunday, November 18, 2012 7:20 AM

Anybody! Please help me properly insert a wpf control in win form.


Sunday, November 18, 2012 11:17 AM

Anybody! Please help me properly insert a wpf control in win form.

Add your  WPF user control to your WF solution.  Build your  solution.  Add an  Element Host and  select your WPF user control.

If you try to insert an Element Host before you add  the WPF user control and click on Edit Hosted Content, you'll get step-by-step instructions.


Sunday, August 25, 2013 1:35 PM

I have the same problem -

I am using vs2010, on x86 (x64 has a bug - cant recognize my clr namespace).

I have added a user control to my workspace by clicking add-->usercontrol (*.xaml)

It is not a separate dll, but part of the program.

I see my user control element in the toolbox - I dragged and dropped it to my main window, however - I can't see it while I run the program!!!

My code is in C#, I have also tried to add them while loading - still doesn't run!

   

xaml:

        <dt:ViewControlButtons Grid.Column="1" Grid.Row="1" HorizontalAlignment="Left" x:Name="viewControlButtons1" VerticalAlignment="Top"  Height="200" Width="400">            
        </dt:ViewControlButtons>

C#:

  private void DynamicParamsWindow_Loaded(object sender, RoutedEventArgs e)
        {
            viewControlButtons1 = new ViewControlButtons();
            this.AddChild(viewControlButtons1);
        }

Anyone?