0

I sorry for my english.

I have a WINFORMS project and within this a WPF User Control.

The WPF shown me correctly, but when I update some controls in WPF not update me such control. (Textbox, labels, image, etc.)

Leave a simple example of a WPF within a Winforms that when you click the button should show in the textbox a "Hello World". But I did not work.

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IntegracionWPF
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(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.
        UserControl1 userControl1 = new UserControl1();
        userControl1.setText("Hola Mundo");

        // Assign the WPF UserControl to the ElementHost control's
        // Child property.
        host.Child = userControl1;

        // Add the ElementHost control to the form's
        // collection of child controls.
        this.Controls.Add(host);
    }
    }
}

userControl1.xaml.cs

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace IntegracionWPF
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }
        public void setText(string text)
        {
            textBox1.Text = text;
        }
    }
}
Fran Pino
  • 111
  • 7

1 Answers1

0

Hope you are looking for Elementhost. It helps you on this. Modify the button click in form1 as below

    private void button1_Click(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.
        UserControl1 userControl1 = new UserControl1();
        userControl1.setText("Hola Mundo");

        // Assign the WPF UserControl to the ElementHost control's
        // Child property.
        host.Child = userControl1;

        // Add the ElementHost control to the form's
        // collection of child controls.
        this.Controls.Add(host);
    }
Uthistran Selvaraj
  • 1,371
  • 1
  • 12
  • 31