Search code examples
c#wpfwinformsuser-controlsmediaelement

how can be added Mediaelement in WPF in windows form


i cant find good ways for embedding a mediaelement in my Winform in C#. I have a PictureBox or Panel in Winform that should be loaded a UserControl in wpf. is this possible at all? if yes ,please give me a litte pseudocode. thanks


Solution

  • You need the ElementHost control in WinForms to host WPF controls

    From that page:

    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);
    }