Update WPF controls within a WinForms with ElementHost

1

I have a project in Winform, which we can no longer migrate to WPF, for reasons of time, we just want one of the forms to be in WPF for reasons of adaptability to different resolutions.

The issue is that I created a WPF user control, and I use ElementHost to show it in a winform.

When I open the window, it shows me without problems, at different resolutions as was the idea. (responsive). But when I update their controls (textbox, label, etc) they do not update me in the window, I tried with usercontrol1.update (); and nothing, returning to show the window and even if I get the same window for the second time it does not come out updated.

Edit: I leave my simplified code to clarify ( userControl.xaml is my WPF with the data to be displayed and MarcadorWPF.cs is my winForm with the element embedded) < strong> panelControl.cs is from I send the updated data.

UserControl.xaml

 public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    public void cargarDatos(String escudoLocal, String escudoVisit, String puntosLocal, String puntosVisit, String nomLocal, String nomVisit)
    {
        BitmapImage bitmapImage = new BitmapImage();
        bitmapImage.BeginInit();
        bitmapImage.UriSource = new Uri(@escudoLocal, UriKind.Absolute);
        bitmapImage.EndInit();
        mImagenLocal.Source = bitmapImage;

        mTxtPuntosLocal.Text = puntosLocal;
        mTxtPuntosVisit.Text = puntosVisit;
        mTxtNomLocal.Text = nomLocal.ToUpper();
        mTxtNomVisit.Text = nomVisit.ToUpper();
    }

panelControl.cs

 public PanelControl()
    {
        InitializeComponent();     
    }
private void btnEnviar_Click(object sender, EventArgs e)
    {
        Equipo equipo = new Equipo();                 
        UserControl1 userControl1 = new UserControl1();
        MarcadorWPF marcadorWPF = new MarcadorWPF();
        userControl1.cargarDatos(pictureBoxLocal.ImageLocation, pictureBoxVisit.ImageLocation, txtLocalPunto.Text, txtVisitPunto.Text, equipo.getNomEquipo(), mTxtNomVisit.Text);
        userControl1.setCrono(txtCrono.Text);
        userControl1.setPointLocal(txtLocalPunto.Text);
        marcadorWPF.Show();
        btnEnviar.Focus();
    }

WPF.cs bookmark

In principle it has nothing, since I use it to show the embedded WPF

EDIT 2: I've done something much simpler to discover the problem. 1. I have made a new project: integracionWPF. 2. I have created an Un form with an ElementHost that embeds a WPF that is called userControl1. 3. I have created a textbox in my WPF (userControl1.xaml) and a button in the Form 4. When pressing the Form button, HOLA MUNDO should appear in my WPF. BUT NOT FUNCTION

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)
        {
            UserControl1 userControl1 = new UserControl1();
            userControl1.setText();
        }
    }
}

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()
        {
            textBox1.Text = "Hola Mundo";
        }
    }
}

Any idea what the problem is? The TextBox1 control is not updated

    
asked by Fran Pino 10.08.2016 в 12:56
source

1 answer

1

As you will notice in the article

Integrate WPF UserControls in WinForms

the control created in wpf must expose some property created by you in order to assign the value to the internal control that it defines.

In your case from the control in wpf you must expose

public void SetText(string text)
{
    textbox1.Text= text;
}

Of course if there are several textboxes you define several methods.

    
answered by 10.08.2016 в 13:19