C# WPF on VB.Net Form

k3nnt0ter0

Member
Joined
Aug 21, 2012
Messages
22
Programming Experience
Beginner
How do I embed the WPF I created in C# to VB.Net Forms? If I remember, the DLL should be added as reference in vb.net. I tried that and I tried to add tools in toolbox but I can't see the WPF I created in toolbox.
 
WPF controls will not show up in Toolbox for a winforms project. In winforms you can add the WPF interop control ElementHost to the form. Then you can do one of two things:
  • add reference to your WPF library dll, and in code create and add the WPF control as Child to the ElementHost control.
  • add the WPF library project to your 'winforms' solution, in winforms project add project reference for the WPF library project, then in forms designer in smart-tag panel for ElementHost you can select the WPF control in 'Select Hosted Content' dropdown. The WPF control will now also show in designer within the ElementHost.
 
ElementHost control is in Toolbox in WPF Interop tab.
 
A. add reference to your WPF library dll, and in code create and add the WPF control as Child to the ElementHost control.

I am not sure how to do this one. .

VB.NET:
 private IApplicationHostWindow _hostWindow;        
        [Obsolete("This constructor should not be used", true)]
        public SampleUserControl()
        {
            InitializeComponent();
        }


        public SampleUserControl(IApplicationHostWindow applicationHostWindow)
        {
            _hostWindow = applicationHostWindow;


            I****TestVisible = true;


            InitializeComponent();
        }


        public double GetViewboxWidth()
        {
            return viewHost.ActualWidth;
        }


        public double GetViewboxHeight()
        {
            return viewHost.ActualHeight;
        }


        public void SetViewboxChild(UIElement child)
        {
            viewHost.Content = child;
        }
Is this what you are talking about? I am not sure how to do this in vb.net
 
In code you typically do something like this:
Me.ElementHost1.Child = New wfplib.usercontrol1
 
I can't get the part usercontrol1. .
I already added as reference my SampleWpfUserControlLibrary.
What else should I add in reference?
 
You don't need other references. You need to use the namespace name and control name that is relevant for your library.
You can also use the other approach I described and IDE will write all code for you, you can then afterwards inspect the generated code to see how it's done :)
 
Arggh! I'll try that later. For the mean time, I'll go to school first :) Thank you! Hope you still help me if I won't be able to do it.
 
Sir JohnH,
Let us begin again. . I am sorry for the very late response. I did many things this past week.

So I have already the Elementhost1. .
Next is making the Elementhost1 as child. I can't make it work.
Says
'Public Sub New()' is obsolete: 'This constructor should not be used
Here's the code
VB.NET:
ElementHost1.Child = New SampleWpfUserControlLibrary.SampleUserControl
I guess I did wrong. After placing Samplewpfusercontrollibrary, the only available code after it are "Resources", "SampleUserControl" and "VisualTreeUtils"
I can't get the UserControl1. . how do I do it?
 
I looks right to me. I don't know the library you're using in detail though.
 
I would guess the message "This constructor should not be used" means that you should not use that constructor, and that the control has one or more other constructors available that require some sort of parameter input.

The namespace, SampleWpfUserControlLibrary, indicates some sort of sample code, it this really your target? It doesn't sound like something really usable.
 
I guess so?
qs2tqv.png

Those were the only content of the Project sir
 
Back
Top