Results 1 to 6 of 6

Thread: Adding WPF controls in WinForm

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2018
    Posts
    39

    Adding WPF controls in WinForm

    I would like to use modern UI controls in my WinForms project and ideally, HandyControl. Since xaml is not used in WinForms, I followed microsoft's Hosting a WPF Composite Control in Windows Forms guide without success.

    MyControls WPF UserControl MyControl1.xaml
    Code:
    <UserControl x:Class="UserControl1"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:MyControls"
                 mc:Ignorable="d" 
                 d:DesignHeight="450" d:DesignWidth="800">
        <UserControl.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml"/>
                    <ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Theme.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </UserControl.Resources>
        <Grid>
                
        </Grid>
    </UserControl>
    WinForms Form1_Load
    Code:
    Dim host = New ElementHost()
    host.Dock = DockStyle.Fill
    Panel1.Controls.Add(host)
    Dim uc = New MyControls.UserControl1
    uc.InitializeComponent()
    host.Child = uc
    All references are included and the projects compile without problems, but I cannot use any WPF or HandyControl controls. Ideally I would like to add HandyControl or WPF controls in my toolbox. Any ideas?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Adding WPF controls in WinForm

    Quote Originally Posted by Pixy View Post
    I cannot use any WPF or HandyControl controls.
    What exactly does that mean? What exactly did you try and what exactly happened?

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2018
    Posts
    39

    Re: Adding WPF controls in WinForm

    I created two projects. One is a wndows form named "Modern UI" and the other is a WPF User Control Library named "MyControls".

    In MyControls I added the nugget package of HandyControl and edited the .xaml of the project accordingly (git).

    In Modern UI I referenced MyControls.dll, PresentationCore, PresentationFramework, System.Xaml, WindowsBase and WindowsFormsIntegration. Also I edited Form1_Load according to the microsoft guide and added a panel control in my form.

    The result, nothing. In Modern UI I don't see any hosted control inside the panel or any new controls in toolbox. I'm sure I've done something wrong.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Adding WPF controls in WinForm

    Quote Originally Posted by Pixy View Post
    I edited Form1_Load according to the microsoft guide
    Which guide do you mean? Are you talking about this:

    https://docs.microsoft.com/en-us/dot...-windows-forms

    That mentions using an ElementHost control on your form, which is what I thought was required. Have you done that?

    This guide on Code Project also uses ElementHost:

    https://www.codeproject.com/articles...e-windows-form

    EDIT: I see that you are referring to an ElementHost in your code in post #1.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Adding WPF controls in WinForm

    This doesn't necessarily solve your problem but I notice that the Code Project article creates a custom WinForms control that inherits ElementHost and does the work internally. That's a good idea, especially if you intend to use the same WPF control multiple times.

  6. #6

    Thread Starter
    Member
    Join Date
    Oct 2018
    Posts
    39

    Re: Adding WPF controls in WinForm

    Thanks, I'll take a look at that article and the source code then. Yes, I intend to use them multiple times. WinForm controls just don't cut it for me anymore. Modern togglebutton sliders and the rest are the new standard, but I do not want to transition to WPF.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width